在设计师中使用一种资源,在生产中使用一种资源。

问题描述 投票:1回答:3

我正在开发Windows Phone 7应用程序,在其中对图像源属性进行数据绑定,并使用转换器生成图像。 像这样:

<Image Source="{Binding Image, Converter={StaticResource MyImageConverter}}"></Image>

这可以在模拟器上运行,但会使设计器崩溃,因为MyImageConverter依赖于设计器未实例化的其他代码。 有什么方法可以使设计人员忽略此属性,甚至更好,我可以指定仅由设计人员使用的另一个Source吗? 如果有问题,我正在使用Visual Studio 2010。

wpf visual-studio-2010 xaml windows-phone-7 designer
3个回答
3
投票

如果“设计师”是指Blend或Cider(VS设计师),则可以使用以下内容查找您是否处于设计模式

public static bool IsInDesignMode
{
    get
    {
        return DesignerProperties.GetIsInDesignMode(new DependencyObject());
    }
}

然后相应地包装您的代码。


1
投票

最好的简单方法是在ViewModel的.ctor中使用IsInDesignMode:

if (IsInDesignMode) {
    //Design time data
} else {
    // production
}

-1
投票

您可以在设计器中而不是在代码中分配Source属性,并使用条件编译:

#if DEBUG
  // assignments for development mode
#else
  // assignments for production mode 
#endif
© www.soinside.com 2019 - 2024. All rights reserved.