包括来自不同程序集的自定义转换器

发布于 2024-10-24 18:11:54 字数 1758 浏览 1 评论 0原文

我正在尝试从不同的程序集实现自定义转换器,但它似乎被忽略了。我已经解决了这个问题,但看不到我的错误,所以也许一些 XAML 忍者可以帮忙。这是相关代码...

 xmlns:converters="clr-namespace:Shared.Converters;assembly=Shared"

以及资源字典...

<ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/Shared;component/Styles.xaml"/>
            <ResourceDictionary>
                <converters:SaveStatusConverter x:Key="saveStateConverter" />
            </ResourceDictionary>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>

这是整个转换器本身。

命名空间 Shared.Converters { 公共类 SaveStatusConverter :IValueConverter {

public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
{

  bool? buttonState = (bool?)value;
  Uri resourceLocater = new Uri("/Shared;component/Styles.xaml", System.UriKind.Relative);
  ResourceDictionary resourceDictionary = (ResourceDictionary)Application.LoadComponent(resourceLocater);
  if(buttonState == true)
    return resourceDictionary["GreenDot"] as Style;
  if (buttonState == false)
    return resourceDictionary["RedDot"] as Style;
  return resourceDictionary["GreyDot"] as Style;

}

public object ConvertBack(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
  throw new System.NotImplementedException();
}

} 这

是我的实现...

<ContentControl Style="{Binding Path=SaveState, Converter={StaticResource saveStateConverter}}"/>

我知道样式有效...这不是问题,我认为转换器也很好,它必须与我尝试调用它的方式有关,尽管我可以没有看到问题...

提前致谢。

I am trying implement a custom converter from a different assembly but it seems to be getting ignored. I have beat this to death and can't see my error so maybe some XAML ninja could help out. Here is the relevant code...

 xmlns:converters="clr-namespace:Shared.Converters;assembly=Shared"

And the resource dictionary...

<ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/Shared;component/Styles.xaml"/>
            <ResourceDictionary>
                <converters:SaveStatusConverter x:Key="saveStateConverter" />
            </ResourceDictionary>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>

Here is the entire converter itself.

namespace Shared.Converters
{
public class SaveStatusConverter : IValueConverter
{

public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
{

  bool? buttonState = (bool?)value;
  Uri resourceLocater = new Uri("/Shared;component/Styles.xaml", System.UriKind.Relative);
  ResourceDictionary resourceDictionary = (ResourceDictionary)Application.LoadComponent(resourceLocater);
  if(buttonState == true)
    return resourceDictionary["GreenDot"] as Style;
  if (buttonState == false)
    return resourceDictionary["RedDot"] as Style;
  return resourceDictionary["GreyDot"] as Style;

}

public object ConvertBack(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
  throw new System.NotImplementedException();
}

}
}

And here is my implementation...

<ContentControl Style="{Binding Path=SaveState, Converter={StaticResource saveStateConverter}}"/>

I know the styles work... that isn't an issue, I think the converter is fine too, it must have something to do with the way I am trying to call it although I can't see the issue...

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

無心 2024-10-31 18:11:54

呃,咕咕咕……我的默认状态名称“灰色”而不是“灰色”有一个拼写错误 - 不管怎样 - 这对于如何做到这一点来说并不是一个太糟糕的例子。 :)

Err, grumble grumble... I had a typo in the name of my default state "gray" not "grey" - Anyway - this is not a too bad example of how to do this. :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文