157

コードが現在デザイン モード (Blend または Visual Studio など) で実行されているかどうかを確認できるように、利用可能なグローバル状態変数を知っている人はいますか?

次のようになります。

//pseudo code:
if (Application.Current.ExecutingStatus == ExecutingStatus.DesignMode) 
{
    ...
}

これが必要な理由は、アプリケーションが Expression Blend のデザイン モードで表示されている場合、ViewModel で代わりに、デザイナーがデザイン モードで表示できるモック データを含む "Design Customer クラス" を使用することです。

ただし、アプリケーションが実際に実行されているときは、当然、ViewModel で実際のデータを返す実際の Customer クラスを使用する必要があります。

現在、デザイナーに作業を依頼する前に、ViewModel に移動し、「ApplicationDevelopmentMode.Executing」を「ApplicationDevelopmentMode.Designing」に変更することで、これを解決しています。

public CustomersViewModel()
{
    _currentApplicationDevelopmentMode = ApplicationDevelopmentMode.Designing;
}

public ObservableCollection<Customer> GetAll
{
    get
    {
        try
        {
            if (_currentApplicationDevelopmentMode == ApplicationDevelopmentMode.Developing)
            {
                return Customer.GetAll;
            }
            else
            {
                return CustomerDesign.GetAll;
            }
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
    }
}
4

9 に答える 9

244

DependencyObject を取るGetIsInDesignModeを探していると思います。

すなわち。

// 'this' is your UI element
DesignerProperties.GetIsInDesignMode(this);

編集: Silverlight / WP7 を使用する場合、Visual Studio で false を返すことがあるIsInDesignToolため、使用する必要があります。GetIsInDesignMode

DesignerProperties.IsInDesignTool

編集:そして最後に、完全を期すために、WinRT / Metro / Windows Store アプリケーションで同等のものは次のDesignModeEnabledとおりです。

Windows.ApplicationModel.DesignMode.DesignModeEnabled
于 2009-05-07T12:20:43.823 に答える
130

次のようなことができます。

DesignerProperties.GetIsInDesignMode(new DependencyObject());
于 2009-05-07T12:19:18.630 に答える
10

this related answer で述べられているように、WPF で設計時データを指定する他の (おそらく新しい) 方法があります。

基本的に、 ViewModelのデザイン時のインスタンスを使用して、デザイン時のデータを指定できます。

d:DataContext="{d:DesignInstance Type=v:MySampleData, IsDesignTimeCreatable=True}"

または、XAML ファイルでサンプル データを指定します

d:DataContext="{d:DesignData Source=../DesignData/SamplePage.xaml}">

SamplePage.xamlファイルのプロパティを次のように設定する必要があります。

BuildAction:               DesignData
Copy to Output Directory:  Do not copy
Custom Tool:               [DELETE ANYTHING HERE SO THE FIELD IS EMPTY]

次のように、これらをUserControlタグに配置します。

<UserControl
    ...
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 

    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    ...
    d:DesignWidth="640" d:DesignHeight="480"
    d:DataContext="...">

実行時には、すべての「d:」デザインタイム タグが消えるため、実行時のデータ コンテキストのみを取得しますが、それを設定することを選択します。

編集 これらの行も必要になる場合があります(確かではありませんが、関連しているようです):

xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
mc:Ignorable="d" 
于 2014-07-21T21:28:29.257 に答える
9

Visual Studioが私のためにコードを自動生成したとき、それは使用しました

if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this)) 
{
    ...
}
于 2012-07-27T15:37:25.693 に答える
2

これは Visual Studio 2013 と .NET 4.5 でしかテストしていませんが、うまくいきます。

public static bool IsDesignerContext()
{
  var maybeExpressionUseLayoutRounding =
    Application.Current.Resources["ExpressionUseLayoutRounding"] as bool?;
  return maybeExpressionUseLayoutRounding ?? false;
}

ただし、Visual Studio の一部の設定でこの値が false に変更される可能性があります。その場合は、このリソース名が存在するかどうかを確認するだけで済みます。nullデザイナーの外でコードを実行したときでした。

このアプローチの利点は、特定のAppクラスの明示的な知識を必要とせず、コード全体でグローバルに使用できることです。具体的には、ビュー モデルにダミー データを入力します。

于 2015-08-26T12:04:20.757 に答える