简体   繁体   English

VisualState Setter中的UWP绑定

[英]UWP Bindings in VisualState Setters

Is it actually possible to use bindings with the Setters Value property in VisualState, because when I try to use x:Bind the initial view of the page is not using the bindings and with Binding the page does not use them ever or do I need to do something additional? 实际上是否可以在VisualState中使用带有Setters Value属性的绑定,因为当我尝试使用x时:绑定页面的初始视图不使用绑定和绑定页面不会使用它们或者我是否需要做些额外的事情?

For example if I use the layout below and I start the app with a width between 400 - 800 the PassInput Passwordbox will not have a placeholder. 例如,如果我使用下面的布局并且我启动宽度在400-800之间的应用程序,则PassInput Passwordbox将没有占位符。 When I resize the window to more than 800 and then back it will finally have the placeholder. 当我将窗口大小调整为800以上然后返回时,它将最终具有占位符。

Example: 例:

<Page
    x:Class="UWP.Learning.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    x:Name="Page">
    <RelativePanel Background="White">
        <VisualStateManager.VisualStateGroups>
            <VisualStateGroup x:Name="WindowSizeStates">
                <VisualState x:Name="SmallState">
                    <VisualState.StateTriggers>
                        <AdaptiveTrigger MinWindowWidth="400" />
                    </VisualState.StateTriggers>
                    <VisualState.Setters>
                        <Setter Target="PassInput.PlaceholderText" Value="{x:Bind MyPlaceHolder, Mode=OneWay}" />
                    </VisualState.Setters>
                </VisualState>
                <VisualState x:Name="WideState">
                    <VisualState.StateTriggers>
                        <AdaptiveTrigger MinWindowWidth="800" />
                    </VisualState.StateTriggers>
                    <VisualState.Setters>
                        <Setter Target="PassInput.PlaceholderText" Value="{x:Null}" />
                    </VisualState.Setters>
                </VisualState>
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>
        <TextBlock
            Name="PassLabel"
             RelativePanel.AlignTopWithPanel="True"
             RelativePanel.AlignLeftWithPanel="True"
             RelativePanel.AlignRightWithPanel="True"
             Text="Pass input label"
             HorizontalAlignment="Center" />
        <PasswordBox
             Name="PassInput"
             RelativePanel.AlignLeftWithPanel="True"
             RelativePanel.AlignRightWithPanel="True"
             RelativePanel.Below="PassLabel"
             VerticalAlignment="Center"
             Margin="20,0" />
    </RelativePanel>
</Page>

Code behind: 代码背后:

namespace UWP.Learning
{
    using Windows.UI.Xaml.Controls;

    public sealed partial class MainPage : Page
    {
        public MainPage() { this.InitializeComponent(); }
        public string MyPlaceHolder => "FOOBAR";
    }
}

Looks like a bug to me. 对我来说看起来像个错误。 An easy workaround is to have a one-time check within the Loaded event. 一个简单的解决方法是在Loaded事件中进行一次性检查。

Loaded += (s, e) =>
{
    switch (ActualWidth)
    {
        case var w when (w >= 400 && w < 800):
            VisualStateManager.GoToState(this, "SmallState", false);
            break;
        case var w when (w >= 800):
            VisualStateManager.GoToState(this, "WideState", false);
            break;
    }
};

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

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