亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

有沒有一種方法可以檢查WPF當前是否在設計模式下執行?

有沒有一種方法可以檢查WPF當前是否在設計模式下執行?

C#
幕布斯6054654 2019-10-15 14:48:58
有誰知道一些可用的全局狀態變量,以便我可以檢查代碼當前是否正在設計模式下執行(例如在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);        }    }}
查看完整描述

3 回答

?
拉風的咖菲貓

TA貢獻1995條經驗 獲得超2個贊

我相信您正在尋找具有DependencyObject的GetIsInDesignMode。


就是


// 'this' is your UI element

DesignerProperties.GetIsInDesignMode(this);

編輯:使用Silverlight / WP7時,應使用,IsInDesignTool因為GetIsInDesignMode有時在Visual Studio中可能返回false:


DesignerProperties.IsInDesignTool

編輯:最后,出于完整性考慮,WinRT / Metro / Windows Store應用程序中的等效項是DesignModeEnabled:


Windows.ApplicationModel.DesignMode.DesignModeEnabled


查看完整回答
反對 回復 2019-10-15
?
至尊寶的傳說

TA貢獻1789條經驗 獲得超10個贊

您可以執行以下操作:


DesignerProperties.GetIsInDesignMode(new DependencyObject());


查看完整回答
反對 回復 2019-10-15
?
躍然一笑

TA貢獻1826條經驗 獲得超6個贊

public static bool InDesignMode()

{

    return !(Application.Current is App);

}

可在任何地方工作。我用它來阻止數據綁定視頻在設計器中播放。


查看完整回答
反對 回復 2019-10-15
  • 3 回答
  • 0 關注
  • 831 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號