【问题标题】:Winrt Dependency Property Visual Studio XAML error.Winrt 依赖属性 Visual Studio XAML 错误。
【发布时间】:2016-08-01 16:50:34
【问题描述】:

这是我的依赖属性:

public static readonly DependencyProperty ButtonTapSoundProperty = DependencyProperty.RegisterAttached("ButtonTapSound", typeof (Uri), typeof (ButtonDependencyObject), new PropertyMetadata(default(Uri), UriChanged));

然后我像这样使用它:

<Button buttonDependencyObject:ButtonDependencyObject.ButtonTapSound="{Binding ElementName=TapSound}" ... />

这在设计时和运行时都能完美运行。

但是,如果我在这样的控件模板中定义它:

<ControlTemplate x:Name="TapSound" TargetType="Button">
      <Button buttonDependencyObject:ButtonDependencyObject.ButtonTapSound="{Binding ElementName=TapSound}" ...  />
 </ControlTemplate>

它在运行时有效,但在 Visual Studio 设计器中无效

【问题讨论】:

  • 只是一个问题:为什么还要在 Button 的 ControlTemplate 中放置另一个按钮?
  • 你为什么在乎它在设计器中不起作用?
  • @lok​​usking 它发生在任何控件模板中。
  • @MichaelThePotato 因为我愿意。这很烦人。
  • 到底发生了什么?您希望播放的声音吗?你如何在设计时触发它?你在设计器中遇到错误吗?

标签: c# visual-studio windows-runtime winrt-xaml


【解决方案1】:

由于缺少进一步的源代码,我只能参考msdn关于dependency properties的实现指南。

为从Button 派生的声音按钮创建一个单独的类,例如“SoundButton”,并使用 getter 和 setter 注册您的属性。

class SoundButton : Button
{
    public Uri ButtonTapSound
    {
        get { return (Uri)GetValue(ButtonTapSoundProperty); }
        set { SetValue(ButtonTapSoundProperty, value); }
    }

    public static readonly DependencyProperty ButtonTapSoundProperty =
        DependencyProperty.Register("ButtonTapSound", typeof(Uri), typeof(SoundButton), new PropertyMetadata(default(Uri), new PropertyChangedCallback(OnUriChanged)));

    private static void OnUriChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
       //Your code
    }
}

然后你可以在你的代码中使用它,而无需在 xaml 中注册依赖属性:

 <local:SoundButton ButtonTapSound="{Binding ElementName=TapSound}"></local:SoundButton>

这可能不是你的风格,但应该解决设计师的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-23
    • 1970-01-01
    • 2015-08-25
    • 1970-01-01
    • 2018-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多