在Windows 10 UWP中具有自定义附加属性的自适应触发器

时间:2015-08-07 08:30:03

标签: xaml windows-10 uwp

我试图在视觉状态中设置一个自定义附加属性我已经尝试了几个完整命名空间的方法,没有,使用别名等等没有成功任何想法?

xmlns:p="using:Controls.Views.Properties"
<RelativePanel x:Name="RootPanel" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <VisualStateManager.VisualStateGroups>
        <VisualStateGroup x:Name="VisualStateGroup">
            <VisualState x:Name="NarrowView">
                <VisualState.StateTriggers>
                    <AdaptiveTrigger MinWindowWidth="0" />
                </VisualState.StateTriggers>
                <VisualState.Setters>
     //This is where the value is not achieved
     <Setter Target="Text.(p:RelativeSize.Width)" Value="0.5"/>

它不会触发任何错误,我可以用j代替p:并且它不会崩溃,我不知道如何解决。

这个问题是在这里完成解决方案:Relative width for UI Elements with RelativePanel in XAML with UWP Apps

2 个答案:

答案 0 :(得分:2)

有一种方法可以使用事件和反射:

XAML:

<VisualStateGroup x:Name="VisualStateGroup" CurrentStateChanged="VisualStateGroup_CurrentStateChanged">

代码:

private void VisualStateGroup_CurrentStateChanged(object sender, VisualStateChangedEventArgs e)
    {
        foreach (var sbase in e.NewState.Setters)
        {
            var setter = sbase as Setter;
            var spath = setter.Target.Path.Path;
            var element = setter.Target.Target as FrameworkElement;

            if (spath.Contains(nameof(RelativeSize)))
            {
                string property = spath.Split('.').Last().TrimEnd(')');

                var prop = typeof(RelativeSize).GetMethod($"Set{property}");

                prop.Invoke(null, new object[] { element, setter.Value });
            }
        }
    }

它解决了这种情况,你可以适应多种情况

答案 1 :(得分:-2)

短代码是正确的。 RelativePanel是根元素吗?

VisualStateManager必须放在根元素中:

<Page ...>
    <Grid x:Name="LayoutRoot">
        <VisualStateManager.VisualStateGroups>
            <VisualStateGroup x:Name="VisualStateGroup">
                <VisualState x:Name="NarrowView">
                    <VisualState.StateTriggers>
                        <AdaptiveTrigger MinWindowWidth="0" />
                    </VisualState.StateTriggers>
                    <VisualState.Setters>//This is where the value is not achieved
                        <Setter Target="Text.(p:RelativeSize.Width)" Value="0.5" />
                        ...
                    </VisualState.Setters>
                </VisualState>
        </VisualStateManager.VisualStateGroups>
    </Grid>
</Page>