簡體   English   中英

如何將DynamicResource的值發送到自定義控件C#后端?

[英]How can I send the value of a DynamicResource to my custom controls C# back end?

我創建了一個自定義控件:

<?xml version="1.0" encoding="UTF-8"?>
<Frame xmlns="http://xamarin.com/schemas/2014/forms" 
   xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"          
   xmlns:local="clr-namespace:Japanese;assembly=Japanese" 
   x:Class="Ja.Templates.Button" 
   x:Name="this" 
   Test="{DynamicResource Test}" >
</Frame>

namespace Japanese.Templates
{
    public partial class Button : Frame
    {

        public static readonly BindableProperty TestProperty = 
           BindableProperty.Create(nameof(Test), typeof(string), typeof(Button), 
           default(string), propertyChanged: TestChanged);

        public string Test { get => (string)GetValue(TestProperty); 
                             set => SetValue(TestProperty, value); }

        private static void TestChanged(BindableObject bindable, 
           object oldValue, object newValue)
        {
            var x = 2;
        }

我的問題是,當我進行如下更改時:

Application.Current.Resources["Test"] = "X";
Application.Current.Resources["Test"] = "Y";

然后,該控件看不到更改,並且在我調試時它沒有轉到變量x。

DynamicResource值更改時,它將不會通知您的控件。 您可以在官方文檔中了解有關此用途的更多信息。 我還建議您在此處閱讀有關BindableProperties 信息

在共享代碼示例中,您不清楚要做什么,但是,似乎應該通過將值存儲在ViewModel來替換DynamicResource

暫無
暫無

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

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