gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 17:42:51 25 4
gpt4 key购买 nike

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

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

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

关于xaml - Windows 10 UWP 中具有自定义附加属性的自适应触发器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31873052/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com