加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

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

发布时间:2020-12-14 02:17:48 所属栏目:Windows 来源:网络整理
导读:我试图在视觉状态中设置一个自定义附加属性我已经尝试了几种方式与完整的命名空间,没有,别名,等没有成功任何想法? xmlns:p="using:Controls.Views.Properties"RelativePanel x:Name="RootPanel" Background="{ThemeResource ApplicationPageBackgroundTheme
我试图在视觉状态中设置一个自定义附加属性我已经尝试了几种方式与完整的命名空间,没有,别名,等没有成功任何想法?

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

解决方法

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

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读