簡體   English   中英

如何移動App.xaml而不破壞設計器?

[英]How to move App.xaml and not to break designer?

我有WPF項目,並在App.xaml中定義了一些其他文件中使用的資源。 當我嘗試將App.xaml移到子目錄設計器時,不再能夠找到那些資源。 由於我使用'Startup'事件而不是'StartupUri',所以我的項目仍然可以編譯。 如何告訴設計師在哪里尋找資源? 當App.xaml位於項目根目錄時,如何知道它們在哪里?

更新:項目文件:

App.xaml中:

<Application x:Class="WpfApplication1.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             Startup="startup">
    <Application.Resources>
        <SolidColorBrush Color="LightGray" x:Key="brush" />
    </Application.Resources>
</Application>

App.xaml.cs

namespace WpfApplication1
{
    public partial class App : System.Windows.Application
    {
        private void startup(object sender, System.Windows.StartupEventArgs e)
        {
            new MainWindow().Show();
        }
    }
}

MainWindow.xaml:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Height="300" Width="300" Background="{StaticResource brush}" />

MainWindow.xaml.cs

namespace WpfApplication1
{
    public partial class MainWindow : System.Windows.Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }
}

更新2:將壓縮的解決方案上傳到http://zalil.ru/30771604 (下載將自動開始)

鏡像: http : //www.speedyshare.com/files/27697250/WpfApplication1.zip

1 將您的App.xaml移至所需位置

App.xaml已移動

2 重構您的App.xaml.cs命名空間以適應新更改:

重構其名稱空間

3 重建您的解決方案。

[4]。 轉到項目屬性,然后在新位置將Startup對象設置為App.xaml文件。

將啟動對象設置為您的App.xaml

[5]。 運行您的應用程序,它應該可以成功運行:)

我無法重復這個問題。 這是我嘗試過的:

我在App.xaml的資源中創建了具有樣式的應用程序。 我將App.xaml移到了子目錄。 我有另一個窗口,該窗口使用App.xaml資源中定義的樣式(他位於其他目錄中),它能夠很好地解決它。 我在原始StartupUri的開頭添加了.. \\。

我做了一些搜索,您正在使用什么版本的Visual Studio? 顯然,VS2008中可能存在與您的問題有關的錯誤:

http://bengribaudo.com/blog/2010/08/19/106/bug-single-application-resources-entry-ignored

他說,解決此錯誤的方法是在Application上設置x:Name屬性。 希望有幫助!

編輯:我也嘗試處理Startup事件而不是使用StartupUri ,它仍然可以正常工作。

public partial class App : Application
{
     private void Application_Startup(object sender, StartupEventArgs e)
     {
          new MainWindow().Show();
     }
}

編輯第2部分:

好的,我將SolidColorBrush封裝在ResourceDictionary中,如下所示:

<ResourceDictionary>
     <SolidColorBrush Color="LightGray" x:Key="brush" /> 
</ResourceDictionary>

然后窗戶拿起刷子。 設計器不喜歡它,但是當我從StaticResource更改為DynamicResource時,它不再抱怨。

編輯3:

我只是想到了什么。 是否已安裝VS2010 SP1? 它修復了設計師的一些錯誤

很抱歉,我的編輯編號2未能按預期工作。 我注意到我的藍色曲線在xaml中消失了,但是我沒有檢查設計師。 x__x

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM