簡體   English   中英

添加舊版 Prism 后,為 WPF 加載了兩個主要 windows

[英]Two main windows loaded for WPF after adding legacy Prism

我的應用程序使用非常舊的 Prism v4.x 參考並添加了區域管理器和模塊時遇到問題。 結果,它顯示了兩個 windows 並且它們鏈接在一起,因此對第一個屏幕中的第二個節目執行任何操作或第一個屏幕未完全加載。

App.xaml.cs 文件:

using System;
using System.Windows;
namespace WpfTestApp
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        [STAThread]
        public static void Main(string[] args)
        {
            try
            {
                var app = new App();
                app.InitializeComponent();
                app.Run();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Fatal Exception occurred!");
            }
        }

        /// <inheritdoc />
        protected override void OnStartup(StartupEventArgs e)
        {
            try
            {
                base.OnStartup(e);
                
                var bootstrapper = new AppBootstrapper();
                bootstrapper.Run();

                base.OnStartup(e);
            }
            catch (Exception ex)
            {
                try
                {
                    MessageBox.Show(ex.ToString(), "App startup");
                }
                finally
                {
                    Current.Shutdown();
                }
            }
        }
    }
}

AppBootstrapper.cs 文件:

using System.Windows;
using Microsoft.Practices.Prism.Modularity;
using Microsoft.Practices.Prism.UnityExtensions;
using Microsoft.Practices.ServiceLocation;

namespace WpfTestApp
{
    public class AppBootstrapper : UnityBootstrapper
    {
        protected override DependencyObject CreateShell()
        {
            InitializeModules();
            return ServiceLocator.Current.GetInstance<MainWindow>();
        }

        /// <summary>
        /// Runs the initialization steps to ensure that the shell is ready to be displayed. The shell application
        /// object is created and set as the main window of the application.
        /// </summary>
        protected override void InitializeShell()
        {
            base.InitializeShell();

            App.Current.MainWindow = (Window)Shell;
            App.Current.MainWindow.Show();
        }

        protected override void ConfigureModuleCatalog()
        {
            ModuleCatalog.AddModule(new ModuleInfo()
            {
                ModuleName = OutlookModule.NAME,
                ModuleType = typeof(OutlookModule).AssemblyQualifiedName,
                InitializationMode = InitializationMode.WhenAvailable
            });
        }
    }
}

虛擬模塊 OutlookModule.cs 文件:

using Microsoft.Practices.Prism.Modularity;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.Unity;

namespace WpfTestApp
{
    [Module(ModuleName = NAME)]
    public class OutlookModule : IModule
    {
        internal const string NAME = "OutlookModule";

        public OutlookModule(IUnityContainer container, IRegionManager regionManager)
        {
        }
        public void Initialize()
        {
        }
    }
}

一個非標准的 WPF 工程文件:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup Label="Globals">
    <SccProjectName>SAK</SccProjectName>
    <SccProvider>SAK</SccProvider>
    <SccAuxPath>SAK</SccAuxPath>
    <SccLocalPath>SAK</SccLocalPath>
  </PropertyGroup>
  <PropertyGroup>
    <TargetFramework>net48</TargetFramework>
    <OutputType>WinExe</OutputType>
    <RootNamespace>SpreadsheetOwnerErrorSampleV3</RootNamespace>
    <AssemblyName>SpreadsheetOwnerErrorSampleV3</AssemblyName>
    <GenerateAssemblyInfo>False</GenerateAssemblyInfo>
    <ExpressionBlendVersion>12.0.51020.0</ExpressionBlendVersion>
    <OutputPath>..\bin\</OutputPath>
    <StartupObject>WpfTestApp.App</StartupObject>
    <EnableDefaultEmbeddedResourceItems>false</EnableDefaultEmbeddedResourceItems>
    <EnableDefaultCompileItems>false</EnableDefaultCompileItems>
    <Prefer32Bit>true</Prefer32Bit>
    <DebugType>full</DebugType>
    <DebugSymbols>True</DebugSymbols>
  </PropertyGroup>
    <ItemGroup>
        <None Remove="WpfTestApp.csproj.vspscc" />
    </ItemGroup>
  <ItemGroup>
      <Page Include="App.xaml">
      <Generator>MSBuild:Compile</Generator>
      <SubType>Designer</SubType>
    </Page>
    <Compile Include="App.xaml.cs">
      <DependentUpon>App.xaml</DependentUpon>
      <SubType>Code</SubType>
    </Compile>
    <Compile Include="AppBootstrapper.cs" />
    <Compile Include="OutlookModule.cs" />
    <Page Include="MainWindow.xaml">
      <Generator>MSBuild:Compile</Generator>
      <SubType>Designer</SubType>
    </Page>
    <Compile Include="MainWindow.xaml.cs">
      <DependentUpon>MainWindow.xaml</DependentUpon>
      <SubType>Code</SubType>
    </Compile>
  </ItemGroup>
  <ItemGroup>
    <AppDesigner Include="Properties\" />
  </ItemGroup>

    <ItemGroup>
        <Reference Include="System" />
        <Reference Include="Microsoft.CSharp" />
        <Reference Include="System.Core" />
        <Reference Include="System.Xaml" />
        <Reference Include="UIAutomationProvider" />
        <Reference Include="WindowsBase" />
        <Reference Include="PresentationCore" />
        <Reference Include="PresentationFramework" />
    </ItemGroup>
    <ItemGroup>
        <PackageReference Include="Prism.Desktop.Net40" Version="4.1.1" />
        <PackageReference Include="Prism.UnityExtensions.Net40" Version="4.1.1" />
        <PackageReference Include="System.Windows.Interactivity.WPF" Version="2.0.20525" />
        <PackageReference Include="Unity" Version="2.1.505" />
    </ItemGroup>
  <ItemGroup>
    <BootstrapperPackage Include=".NETFramework,Version=v4.0">
      <Visible>False</Visible>
      <ProductName>Microsoft .NET Framework 4 %28x86 and x64%29</ProductName>
      <Install>true</Install>
    </BootstrapperPackage>
  </ItemGroup>
  <PropertyGroup>
    <LanguageTargets>$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Bin\Microsoft.CSharp.targets</LanguageTargets>
    <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
  </PropertyGroup>
  <Target Name="WorkaroundForXAMLIntellisenseBuildIssue" AfterTargets="_CheckCompileDesignTimePrerequisite">
    <PropertyGroup>
      <BuildingProject>false</BuildingProject>
    </PropertyGroup>
  </Target>
  <Choose>
    <When Condition="'$(MSBuildProjectExtension)' != '.csproj' and '$(MSBuildProjectExtension)' != '.vbproj'">
      <PropertyGroup>
        <LanguageTargets Condition="Exists('$(MSBuildProjectDirectory)\$(AssemblyName).csproj')">$(MSBuildToolsPath)\Microsoft.CSharp.targets</LanguageTargets>
        <LanguageTargets Condition="Exists('$(MSBuildProjectDirectory)\$(AssemblyName).vbproj')">$(MSBuildToolsPath)\Microsoft.VisualBasic.targets</LanguageTargets>
      </PropertyGroup>
    </When>
  </Choose>
</Project>

請幫助確定為什么兩個 Windows 應該只是第一個加載。 我知道使用的棱鏡已經過時了,我們將來會升級。 但是,我需要一個基於此版本的工作示例。 所有這些遺留代碼都是基於不久前的休閑文章實現的:

http://www.infragistics.com/community/blogs/blagunas/archive/2012/09/13/xamdockmanager-a-prism-regionadapter.aspx Z80791B3AE7002CB88C246876D9FAA8F8-region-adamdockmanages.com/ / http://brianlagunas.com/xamdockmanager-prism-region-adapter-update-3/

windows 在 CreateShell 或更高版本上執行 app.Run() 期間從 App.xaml.cs Main 方法加載。 但是,根據文檔,這是應用程序所必需的: https://prismlibrary.com/docs/wpf/legacy/Initializing.html

提前致謝

正如@mm8 建議的那樣,問題似乎出在 App.xaml 本身,默認情況下,它會在主視圖中注入啟動行:

<Application x:Class="WpfTestApp.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:WpfTestApp"

             !-> StartupUri="MainWindow.xaml">

    <Application.Resources>
         
    </Application.Resources>
</Application>

暫無
暫無

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

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