简体   繁体   English

DataTrigger链接到资源。 Xamarin表格

[英]DataTrigger linked to resourse. Xamarin.Forms

Need to change Page Background . 需要更改页面背景 I have such class: 我有这样的课:

class DataClass:INotifyPropertyChanged
{
    private string currentTheme;

    public string CurrentTheme
    {
        get { return currentTheme; }
        set
        {
            currentTheme = value;
            OnPropertyChanged("CurrentTheme");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    public void OnPropertyChanged(string property)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
    }
}

Have Data and Style in ResourseDictionary of my app: 在我的应用程序的ResourseDictionary中具有数据样式

<local:DataClass x:Key="Data" CurrentTheme="Default"/>

        <!-- Styles -->

        <Style x:Key="Page"
               TargetType="ContentPage">

            <Style.Triggers>

                <!-- Need to bind it to Data-->
                <DataTrigger >
                  <Setter Property="BackgroundColor"
                          Value="Aqua"/>
                </DataTrigger>

            </Style.Triggers>

        </Style>

Can't bind DataTrigger to Data.CurrentTheme . 无法将DataTrigger绑定到Data.CurrentTheme Tried BindingContext but mostly have errors. 尝试了BindingContext,但是大多数都有错误。 Pls, help 请帮助

If you are trying to theme your application and needs to change the theme in between, then you will be better of using the new Xamarin.Forms Themes . 如果您尝试为应用程序设置主题并需要在两者之间更改主题,那么最好使用新的Xamarin.Forms主题 You can either use the pre built light and dark theme via nuget or create your own local themes and use it through out your application. 您可以通过nuget使用预制的明暗主题,也可以创建自己的本地主题并在整个应用程序中使用它。

Creating a Custom Theme walks you through all the steps in detail on how to create the theme and use it. 创建自定义主题将带您详细了解如何创建和使用主题的所有步骤。

DataTriggers are very similar to PropertyTriggers, except that instead of specifying the Property, we specify the Binding for the trigger. DataTriggers与PropertyTriggers非常相似,除了我们指定触发器的绑定而不是指定Property之外。 This Binding generally refers to another VisualElement's property on the page or it could reference a property in a ViewModel. 此绑定通常引用页面上另一个VisualElement的属性,或者可以引用ViewModel中的属性。

Eg : To disable the button when the entry's Text.Length property is 0. 例如:当条目的Text.Length属性为0时,禁用按钮。

<StackLayout Spacing="20">
<Entry x:Name="emailAddress" Text="" Placeholder="email address"/>
<Button Text="Send">
  <Button.Triggers>
    <DataTrigger TargetType="Button"
         Binding="{Binding Source={x:Reference emailAddress},Path=Text.Length}" Value="0">
      <Setter Property="IsEnabled" Value="False" />
    </DataTrigger>
  </Button.Triggers>
</Button>
</StackLayout>

If you need more information then read Data Triggers and Triggers in Xamarin.Forms 如果需要更多信息,请阅读Xamarin.Forms中的 数据触发器触发器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM