Xamarin UserControl不在XAML中加载,但在代码隐藏中工作
我试图通过引用dll来使用在解决方案外部定义的usercontrol。如果我使用代码,它就会工作;但不是XAML 这是密码- 1.用户控制代码Xamarin UserControl不在XAML中加载,但在代码隐藏中工作,xaml,xamarin,user-controls,portable-class-library,Xaml,Xamarin,User Controls,Portable Class Library,我试图通过引用dll来使用在解决方案外部定义的usercontrol。如果我使用代码,它就会工作;但不是XAML 这是密码- 1.用户控制代码 using System; using System.Collections.Generic; using System.Linq; using System.Text; using Xamarin.Forms; using Xamarin.Forms.Internals; namespace ComplexControl { [Preserv
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 { }
}
}
<?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>
我已经尝试过调试thru,但没有看到它将用户控件添加到内容中
有人能帮我看看我做错了什么吗?
如果我不将XAML用于自定义控件,它是有效的
我有一个usercontrol工作,其中usercontrol是使用代码完成的;但它将由XAML内部的项目引用
谢谢,XP,您做的每件事都是正确的 不幸的是,小的错误类型(:而不是=)导致XAML定义的控件出现此问题
xmlns:custom=“clr命名空间:ComplexControl;程序集:ComplexControl”
应该是
xmlns:custom=“clr命名空间:ComplexControl;assembly=ComplexControl”
isclr名称空间的语法:名称空间;组装=组装
我相信Xamarin.Forms应该更聪明地对待名称空间,并在编辑器中或构建时警告您此类错误类型(Xamarin.Forms默认情况下不编译XAML,但没有人阻止它运行简单的语法检查,对吧?)
如果您愿意,您可以(很容易!)App.xaml.cs有这段代码。只是以防万一需要
public App(){InitializeComponent();MainPage=new CustomControlUsageExample.MainPage();}
谢谢,我不再解析了。但内容仅显示标签,而按钮未显示。不知怎的,堆栈只显示了1个视图的计数,这就是标签,这两个都是我的:下面是一个示例解决方案:让Xaml创建内容,然后代码隐藏覆盖该Xaml内容。我要寻找的是嵌入到xaml脚本中的ComplexControl,代码背后应该只有一条示例语句。这就是InitializeComponent();对不起,我的错。再看一遍。我看到您正在运行另一个Xaml视图。我正试图让它在我的系统上运行。Windows 10 VS 2015如果您需要帮助,请在聊天中与我聊天,@XP。
<?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();
}
}
}