繁体   English   中英

WPF设计时类型投放错误

[英]WPF Design-Time Type-Cast error

我有一个具有Singleton模式的Settings类:

public class Settings
{
    public static Settings Instance 
    {
         get { ... /* Return instance created by custom xml deserializer*/ }
    } 
    public Settings() 
    {
        /* Constructor used by custom xml deserializer */
    }

    public bool EnableImages { get; set; }
    public bool CheckForUpdates { get; set; }
    public ObservableCollection<FavShowData> TvShows {get; set;}
    //....
}

在我的设置表单中,我绑定到这些设置:

<CheckBox IsChecked="{Binding CheckForUpdates, Source={x:Static local:Settings.Instance}}"  Content="Autocheck at start and every 30 minutes" />
<CheckBox IsChecked="{Binding EnableImages, Source={x:Static local:Settings.Instance}}"  Content="Enable Images" />

对于这两行,我都收到以下设计时错误:

类型“ System.Collections.ObjectModel.ObservableCollection 1[SjUpdater.Model.FavShowData]" kann nicht in den Typ "System.Collections.ObjectModel.ObservableCollection 1 [SjUpdater.Model.FavShowData]”的示例。

已翻译:

1[SjUpdater.Model.FavShowData]" can not be converted into type "System.Collections.ObjectModel.ObservableCollection 1 [SjUpdater.Model.FavShowData]”的对象1[SjUpdater.Model.FavShowData]" can not be converted into type "System.Collections.ObjectModel.ObservableCollection 1 [SjUpdater.Model.FavShowData]”的对象

这很奇怪,因为我从未绑定到TvShows属性。 编译很好,代码可以按需工作。

您对如何解决设计时错误有任何建议?

您是否也尝试过将属性设置为静态...您正在工作的静态类不允许实例构造函数,但是您的属性也未标识为静态

public static bool EnableImages { get; set; }
public static bool CheckForUpdates { get; set; }
public static ObservableCollection<FavShowData> TvShows {get; set;}

解决方法:将资料移至ViewModel:

public class MainWindowViewModel
{
    public Settings Settings
    {
        get { return Settings.Instance; }
    }
    //....
}

并将XAML更改为:

<Grid DataContext="{Binding Settings}">
    <CheckBox IsChecked="{Binding CheckForUpdates}"  Content="Autocheck at start and every 30 minutes" />
    <CheckBox IsChecked="{Binding EnableImages}"  Content="Enable Images" />
    ....
</Grid>

没有更多的设计时错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM