C# 在WPF应用程序中使用UWP UI

C# 在WPF应用程序中使用UWP UI,c#,wpf,xaml,uwp,C#,Wpf,Xaml,Uwp,我有一个在后台运行的应用程序,但我想添加一个带有UWP API的窗口 将引用添加到: Windows.UI Windows.UI.Xaml 我尝试通过以下方式启动UWP应用程序: Windows.UI.Xaml.Application.Start(p => new UWPApp(); ); 在UWPAP类中,我添加了UWP模板代码: using System; using System.Diagnostics; using Windows.ApplicationModel; using

我有一个在后台运行的应用程序,但我想添加一个带有UWP API的窗口

将引用添加到:

Windows.UI
Windows.UI.Xaml
我尝试通过以下方式启动UWP应用程序:

Windows.UI.Xaml.Application.Start(p => new UWPApp(); );
在UWPAP类中,我添加了UWP模板代码:

using System;
using System.Diagnostics;
using Windows.ApplicationModel;
using Windows.ApplicationModel.Activation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;

namespace ClipboardSync {

    public class UWPApp : Windows.UI.Xaml.Application {

        private bool _contentLoaded;

        public void InitializeComponent() {
            if (_contentLoaded)
                return;

            _contentLoaded = true;
        }

        public UWPApp() {
            this.InitializeComponent();
            this.Suspending += OnSuspending;
        }


        protected override void OnLaunched(LaunchActivatedEventArgs e) {
            Frame rootFrame = Window.Current.Content as Frame;

            // Do not repeat app initialization when the Window already has content,
            // just ensure that the window is active
            if (rootFrame == null) {
                // Create a Frame to act as the navigation context and navigate to the first page
                rootFrame = new Frame();

                rootFrame.NavigationFailed += OnNavigationFailed;

                if (e.PreviousExecutionState == ApplicationExecutionState.Terminated) {
                    //TODO: Load state from previously suspended application
                }

                // Place the frame in the current Window
                Window.Current.Content = rootFrame;
            }

            if (e.PrelaunchActivated == false) {
                if (rootFrame.Content == null) {
                    // When the navigation stack isn't restored navigate to the first page,
                    // configuring the new page by passing required information as a navigation
                    // parameter
                    rootFrame.Navigate(typeof(MainPage), e.Arguments);
                }
                // Ensure the current window is active
                Window.Current.Activate();
            }
        }

        /// <summary>
        /// Invoked when Navigation to a certain page fails
        /// </summary>
        /// <param name="sender">The Frame which failed navigation</param>
        /// <param name="e">Details about the navigation failure</param>
        void OnNavigationFailed(object sender, NavigationFailedEventArgs e) {
            throw new Exception("Failed to load Page " + e.SourcePageType.FullName);
        }

        /// <summary>
        /// Invoked when application execution is being suspended.  Application state is saved
        /// without knowing whether the application will be terminated or resumed with the contents
        /// of memory still intact.
        /// </summary>
        /// <param name="sender">The source of the suspend request.</param>
        /// <param name="e">Details about the suspend request.</param>
        private void OnSuspending(object sender, SuspendingEventArgs e) {
            var deferral = e.SuspendingOperation.GetDeferral();
            //TODO: Save application state and stop any background activity
            deferral.Complete();
        }

    }  

}
使用系统;
使用系统诊断;
使用Windows.ApplicationModel;
使用Windows.ApplicationModel.Activation;
使用Windows.UI.Xaml;
使用Windows.UI.Xaml.Controls;
使用Windows.UI.Xaml.Navigation;
名称空间剪贴板同步{
公共类UWPApp:Windows.UI.Xaml.Application{
私有bool_内容加载;
public void InitializeComponent(){
如果(_contentLoaded)
返回;
_contentLoaded=true;
}
公共UWPAP(){
this.InitializeComponent();
这个.Suspending+=OnSuspending;
}
仅启动受保护的覆盖无效(启动ActivatedEventArgs e){
Frame rootFrame=Window.Current.Content作为Frame;
//当窗口已经有内容时,不要重复应用程序初始化,
//只需确保窗口处于活动状态
if(rootFrame==null){
//创建一个框架作为导航上下文并导航到第一页
rootFrame=新框架();
rootFrame.NavigationFailed+=OnNavigationFailed;
如果(例如,PreviousExecutionState==ApplicationExecutionState.Terminated){
//TODO:从先前挂起的应用程序加载状态
}
//将框架放置在当前窗口中
Window.Current.Content=rootFrame;
}
如果(如预启动激活==错误){
if(rootFrame.Content==null){
//导航堆栈未还原时,请导航到第一页,
//通过将所需信息作为导航传递来配置新页面
//参数
导航(typeof(MainPage),例如参数);
}
//确保当前窗口处于活动状态
Window.Current.Activate();
}
}
/// 
///当导航到某个页面失败时调用
/// 
///导航失败的帧
///有关导航失败的详细信息
void OnNavigationFailed(对象发送方,NavigationFailedEventArgs e){
抛出新异常(“加载页面失败”+e.SourcePageType.FullName);
}
/// 
///在挂起应用程序执行时调用。应用程序状态已保存
///不知道应用程序是否会随内容一起终止或恢复
///记忆仍然完好无损。
/// 
///挂起请求的源。
///有关暂停请求的详细信息。
暂停时的私有void(对象发送方,SuspendingEventArgs e){
var deleral=e.SuspendingOperation.getdeleral();
//TODO:保存应用程序状态并停止任何后台活动
延迟。完成();
}
}  
}
但从未创建新的UWPAP实例,并且没有错误消息


是否存在某种限制,使我无法在WPF应用程序中启动UWP应用程序?

2019年1月31日更新 回到这里更新我的答案。从Windows 10 update 1809(build 17763+)开始,现在支持此功能。更多信息和示例请参见:

旧答案-1809年以前 这在今天是不受支持的。Windows.UI.XAML.*当前只能在UWP应用程序进程中使用

然而,在今年早些时候的BUILD2017上,宣布计划在Windows 10的未来更新中对此提供支持: