5

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

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

4

2 回答 2

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 });
            }
        }
    }

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

于 2015-08-08T08:03:23.273 回答
-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>
于 2015-08-07T09:28:50.373 回答