【问题标题】:Xamarin Forms - LoadFromXaml and referencing containing pageXamarin Forms - LoadFromXaml 和引用包含页面
【发布时间】:2020-04-15 09:25:15
【问题描述】:

我有一个位于 ContentPage 上的 ListView,我希望 ListView 的 ItemTemplate 是用户可定义的 - 即在运行时为模板解析 XAML。但是,我需要为用户提供在页面上调用命令的按钮的选项。见下文 XAML;

 <SwipeView>
<SwipeView.RightItems>
    <SwipeItems>
        <SwipeItem
                            Text = ""Delete""
                            BackgroundColor=""Red""
                            Command=""{Binding Path=ItemDeletedCommand, Source={x:Reference multiEntryPage}}""
                            CommandParameter=""{Binding .}"" />
    </SwipeItems>
</SwipeView.RightItems>
<SwipeView.Content>
                ...
</SwipeView.Content>

我的内容页面在其 ContentPage 元素中有 x:Name="multiEntryPage" 属性。我用来解析 XAML 的代码如下所示:

listView.ItemTemplate = new DataTemplate(() => new ViewCell().LoadFromXaml(xaml));

但是,这会产生以下错误:

{Xamarin.Forms.Xaml.XamlParseException:位置 11:33。找不到 multiEntryPage 引用的对象在 Xamarin.Forms.Xaml.ApplyPropertiesVisitor.ProvideValue (System.Object& 值,Xamarin.Forms.Xaml.ElementNode 节点,System.Object 源, Xamarin.Forms.Xaml.XmlName propertyName)...

所以我认为很明显错误正在发生,因为 SwipeView XAML 在这个阶段不知道它的父级,但我不知道如何解决这个问题。有什么想法吗?

谢谢!

【问题讨论】:

  • xaml 的值是多少?你是怎么得到的?能否提供完整代码?
  • xaml的值是第一部分代码sn-p中列出的xaml
  • 问题解决了吗?
  • 不,我决定以不需要 SwipeView 的不同方式解决我的问题

标签: c# xaml xamarin.forms


【解决方案1】:

您可以通过以下方式创建DataTemplate

  • 创建内联数据模板

  • 使用类型创建 DataTemplate

  • 将 DataTemplate 创建为资源

请参考https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/templates/data-templates/creating

【讨论】:

    猜你喜欢
    • 2016-09-22
    • 2014-09-13
    • 1970-01-01
    • 2017-12-29
    • 2017-09-22
    • 1970-01-01
    • 1970-01-01
    • 2019-07-19
    • 1970-01-01
    相关资源
    最近更新 更多