簡體   English   中英

將 object 分配給 ResourceDictionary 中的 BindableProperty

[英]Assign an object to a BindableProperty in ResourceDictionary

我嘗試初始化自定義 class 的實例並將其分配給ResourceDictionary (styles.xaml) 中的BindableProperty

我的 class:

namespace Foo {
  public class Colors {
    public Color Positive { get; set; }
    public Color Negative { get; set; }
    public Color Neutral { get; set; }

    public Colors() { }
  }
}

我的 class 帶有 BindableProperty:

namespace Foo {
  public class Bar : ContentView {
    public Colors Colors {
      get => (Colors)GetValue(ColorsProperty);
      set => SetValue(ColorsProperty, value);
    }

   public static BindableProperty ColorsProperty = 
            BindableProperty.Create(nameof(Colors), typeof(Colors), 
            typeof(Bar), null, BindingMode.OneWay);

styles.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ResourceDictionary xmlns="http://xamarin.com/schemas/2014/forms"
                    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                    xmlns:foo="clr-namespace:Foo;assembly=Foo">
  <foo:Colors
        x:Key="colors"
        Positive="Green"
        Negative="Red"
        Neutral="Blue"/>

  ...

  <Style TargetType="foo:Bar">
    <Setter Property="Colors" Value="{StaticResource colors}"/>
  </Style>
</ResourceDictionary>

A breakpoint in the constructor of class Foo.Colors is hit, so an instance of this class is created during loading styles.xaml. 但是加載 styles.xaml 會崩潰,並在 Mono.ZE84E30B9390CDB664DDB6DB2.C 中出現未處理的NullReferenceException 當我刪除顏色屬性的分配時,一切正常,所以分配一定是問題所在。

有什么建議么?

異常詳情:

System.NullReferenceException: Object reference not set to an instance of an object.
  at Android.Runtime.JNINativeWrapper._unhandled_exception (System.Exception e) [0x0000e] in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:12 
  at Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PPL_V (_JniMarshal_PPL_V callback, System.IntPtr jnienv, System.IntPtr klazz, System.IntPtr p0) [0x0001d] in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:111 
  at (wrapper native-to-managed) Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PPL_V(intptr,intptr,intptr)

The Exception occures in App.xaml.g.cs, after global::Xamarin.Forms.Xaml.Extensions.LoadFromXaml(this, typeof(App));

我找到了一個適合我的簡單解決方案:

在 styles.xaml 我用DynamicResource替換了StaticResource

<Style TargetType="foo:Bar">
  <Setter Property="Colors" Value="{DynamicResource colors}"/>
</Style>

感謝所有建議!

看起來您想要使用獨立的資源字典。 過去,當我遵循 Microsoft 的文檔時,我遇到過這些問題。 我總是不得不像在另一個程序集中一樣設置字典:

styles.xaml:

<ResourceDictionary
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:foo="clr-namespace:DELETEME"
    x:Class="DELETEME.styles">

    <foo:Colors
        x:Key="colors"
        Positive="Green"
        Negative="Red"
        Neutral="Blue"/>

    <Style TargetType="foo:Bar">
        <Setter Property="Colors" Value="{StaticResource colors}"/>
    </Style>
</ResourceDictionary>

請注意,此文件確實需要一個簡單的代碼:

namespace DELETEME
{
    public partial class styles : ResourceDictionary
    {
        public styles()
        {
            InitializeComponent();
        }
    }
}

添加此文件的最簡單方法是添加帶有 xaml 的 ContentView,然后將 xaml 和 cs 更改為從 ResourceDictionary 繼承。

從那里,將文件添加到應用程序或頁面的合並字典中:

<Application xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:foo="clr-namespace:DELETEME"
         x:Class="DELETEME.App">

    <Application.Resources>
       <ResourceDictionary>
           <ResourceDictionary.MergedDictionaries>
               <foo:styles />
           </ResourceDictionary.MergedDictionaries>
       </ResourceDictionary>
    </Application.Resources>
</Application>

這應該可以解決您的錯誤。 下面的例子。

Bar.cs class:

public class Bar : ContentView
{
    public static readonly BindableProperty ColorsProperty =
        BindableProperty.Create(nameof(Colors), typeof(Colors), typeof(Bar));

    public Colors Colors
    {
        get => (Colors)GetValue(ColorsProperty);
        set => SetValue(ColorsProperty, value);
    }

    public Bar()
    {
        var b1 = new BoxView { BackgroundColor = Colors.Negative };
        var b2 = new BoxView { BackgroundColor = Colors.Neutral };
        var b3 = new BoxView { BackgroundColor = Colors.Positive };

        var sl = new StackLayout();

        sl.Children.Add(b1);
        sl.Children.Add(b2);
        sl.Children.Add(b3);

        Content = sl;
    }
}

主頁.xaml class:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:foo="clr-namespace:DELETEME"
         x:Class="DELETEME.MainPage">

    <StackLayout>
        <foo:Bar />
    </StackLayout>

</ContentPage>

運行結果:

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM