Xamarin UserControl无法在XAML中加载,但在代码后面工作
我想通过引用dll来使用在解决方案外部定义的usercontrol。它使用代码;但不XAMLXamarin UserControl无法在XAML中加载,但在代码后面工作
下面是代码 - 1.用户控件代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xamarin.Forms;
using Xamarin.Forms.Internals;
namespace ComplexControl
{
[Preserve]
public class ComplexControl : ContentView
{
public ComplexControl()
{
var button = new Button
{
Text = "Click Me!",
//VerticalOptions = LayoutOptions.CenterAndExpand,
HorizontalOptions = LayoutOptions.FillAndExpand,
};
int clicked = 0;
button.Clicked += (s, e) => button.Text = "Clicked: " + clicked++;
Content = button;
}
[AttributeUsage(AttributeTargets.Class, Inherited = false)]
class PreserveAttribute : Attribute { }
}
}
- 对于项目中的所述使用我参考组装ConplexControl在PCL
它与代码(即不使用XAML)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using ComplexControl;
namespace CustomControlUsageExample
{
public partial class MainPage : ContentPage
{
public MainPage()
{
// without InitializeComponent(); works
//this way works
//Step 1.
StackLayout sl = new StackLayout();
Label lb = new Label();
lb.Text = "Hello";
sl.Children.Add(lb);
sl.Children.Add(new ComplexControl.ComplexControl());
Content = sl;
}
}
}
如果我用XAML中使用它不行
喜欢这个
XAML是
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:CustomControlUsageExample"
x:Class="CustomControlUsageExample.MainPage"
xmlns:custom="clr-namespace:ComplexControl;assembly:ComplexControl">
<StackLayout HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
<Label Text="Welcome to Xamarin Forms!"/>
<custom:ComplexControl.ComplexControl/>
</StackLayout>
</ContentPage>
和代码背后是
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using ComplexControl;
namespace CustomControlUsageExample
{
public partial class MainPage : ContentPage
{
public MainPage()
{
//This way does not work
//Step 2
InitializeComponent();
}
}
}
我已经尝试调试通并没有看到它添加用户控制内容。
有人能帮我看看我做错了什么吗? 这是工程,如果我不使用自定义控件的XAML。
我有一个usercontrol工作正在完成的地方userControl完成使用代码;但它将被XAML内的项目引用。
谢谢,
XP,你正在做的一切正确。
不幸的是,小错误类型(:而不是=)导致您使用XAML定义的控件出现此问题。
xmlns:custom="clr-namespace:ComplexControl;assembly:ComplexControl"
应该
xmlns:custom="clr-namespace:ComplexControl;assembly=ComplexControl"
语法custom XAML namespace是clr-namespace:NAMESPACE;assembly=ASSEMBLY
我相信Xamarin.Forms应该是关于命名空间的更聪明,警告您在编辑器中或在这样的输入错误编译时间(Xamarin.Forms默认不会编译XAML,但没有人会阻止它运行简单的语法检查,对吧?)。
如果你愿意,你可以file a bug to Xamarin(很容易!)
谢谢,我离开解决。但内容仅显示标签,而不是按钮没有显示。不知何故,堆栈显示只有1个视图的计数,这是标签 –
为我显示:https://www.dropbox。com/s/2dqe7b9xgcr1nor /截图%202017-03-17%2011.05.39.png?dl = 0 下面是一个示例解决方案:https://www.dropbox.com/s/i8pp86n4qhl76qg/CustomControlXAML.zip?dl = 0 –
您有Xaml创建内容,然后代码覆盖xaml内容。我正在寻找的是将ComplexControl嵌入到xaml脚本中,并且后面的代码应该只有您的示例中的1条语句。那是InitializeComponent(); –
App.xaml.cs有此代码段。刚开始需要'公共App() { InitializeComponent(); MainPage = new CustomControlUsageExample.MainPage(); }' –