【问题标题】:Xamarin UserControl not loading in XAML but works in code behindXamarin UserControl 未在 XAML 中加载,但在后面的代码中工作
【发布时间】:2017-03-17 00:29:12
【问题描述】:

我正在尝试通过引用 dll 来使用在解决方案之外定义的用户控件。如果我使用代码,它就可以工作;但不是 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 { }
    }
}
  1. 对于项目中的使用,我在 PCL 中引用了程序集 ConplexControl

它适用于代码(即不使用 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 的用户控件工作;但它会被 XAML 中的项目引用。

谢谢,

【问题讨论】:

  • App.xaml.cs 有这个 sn-p 的代码。只是需要public App() { InitializeComponent(); MainPage = new CustomControlUsageExample.MainPage(); }

标签: xaml xamarin user-controls portable-class-library


【解决方案1】:

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 个视图的计数,这就是标签
  • 您让 Xaml 创建内容,然后后面的代码会覆盖该 xaml 内容。我正在寻找的是要嵌入到 xaml 脚本中的 ComplexControl,并且后面的代码应该只有您的示例的 1 个语句。那就是 InitializeComponent();
  • 对不起,我的错。再看一遍。我看到您正在运行另一个 Xaml 视图。我试图让它在我的系统上运行。视窗 10 VS 2015
  • 如果您需要帮助,请在聊天中联系我,@XP。 chat.stackoverflow.com/rooms/138376/xamarin
猜你喜欢
  • 2020-08-16
  • 2019-03-07
  • 1970-01-01
  • 1970-01-01
  • 2020-02-19
  • 1970-01-01
  • 1970-01-01
  • 2018-09-24
  • 1970-01-01
相关资源
最近更新 更多