有没有人在开发控件时找到了解决DesignMode问题的有用方法?
问题是,如果嵌套控件,则DesignMode仅适用于第一级.第二级和更低级别的DesignMode将始终返回FALSE.
标准的hack一直在查看正在运行的进程的名称,如果它是"DevEnv.EXE"那么它必须是studio,因此DesignMode真的是真的.
问题是寻找ProcessName通过注册表和其他奇怪的部分工作,最终结果是用户可能没有查看进程名称所需的权限.另外这条奇怪的路线很慢.所以我们不得不堆积额外的黑客来使用单例,如果在请求进程名称时抛出错误,则假设DesignMode为FALSE.
确定DesignMode的一个很好的干净方法是有序的.让微软在框架内部修复它会更好!
我正在使用WPF.我有一个静态类,它执行一些在设计模式下不可用的设置.此构造函数在设计模式下由窗口调用,这会导致抛出异常.
如何在静态方法中检测设计模式,以便调用适当的设计模式行为?
该建议的方法不适用于静态方法的工作.
编辑:
静态构造函数是从xaml调用的,所以我不能有条件地调用它(除非我将调用移到代码隐藏,我想避免).
在窗口中: <Window ... HelpProvider.Keyword="some_help_topic.html">
在课堂里:
static HelpProvider()
{
// Load the .chm file from an application setting (this fails at design time)
// Add a WPF command binding
}
Run Code Online (Sandbox Code Playgroud) 不幸的是,我发现有时我编写的代码虽然在运行时非常好,但在使用Visual Studio 2010中的XAML/Designer时会让我感到头疼.我最喜欢的例子包括多个MessageBox用于调试出现,但是,当前示例是构造函数中非常轻的Singleton样式条件,这意味着当我想在XAML中更改实例时,我必须重建解决方案.
是否有一个预处理器指令可用于跳过XAML设计器中的代码?
例:
public class CustomFE : FrameworkElement
{
public CustomFE()
{
#if !XAMLDesigner // Or something similar
if (_instance != null)
throw new NotSupportedException("Multiple instances not supported");
#endif
_instance = this;
}
private static CustomFE _instance = null;
public static CustomFE Instance
{
get { return _instance; }
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个基本的WinRT XAML UserControl,它有一个依赖属性,如下所示.用户控件显然只在另一个Page或UserControl中使用时才构造设计时.当我使用用户控件本身在设计器中工作时,不会呈现文本"Hello world".在这种情况下,如何让设计器使用数据初始化用户控件?
XAML:
<UserControl
x:Class="GTWin8.Ui.SimpleBinding"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:GTWin8.Ui"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400"
x:Name="ThisControl">
<Grid Background="Black">
<TextBlock Text="{Binding Path=Message, ElementName=ThisControl}"
HorizontalAlignment="Left" Margin="10,10,0,0"
TextWrapping="Wrap" VerticalAlignment="Top" Height="280" Width="380"/>
</Grid>
</UserControl>
Run Code Online (Sandbox Code Playgroud)
代码隐藏:
public sealed partial class SimpleBinding : UserControl
{
public static DependencyProperty MessageProperty = DependencyProperty.Register(
"Message", typeof(String), typeof(SimpleBinding), new PropertyMetadata(null));
public String Message
{
get { return (String)GetValue(MessageProperty); }
set { SetValue(MessageProperty, value); }
}
public SimpleBinding()
{
this.InitializeComponent();
Message = "Hello world";
}
}
Run Code Online (Sandbox Code Playgroud) wpf ×2
xaml ×2
.net ×1
c# ×1
designmode ×1
silverlight ×1
static ×1
windows-8 ×1
winrt-xaml ×1