You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从UWP应用自身启动同应用新实例?求代码或文档支持

启动UWP应用的新实例(跳过初始步骤直接打开目标页面)

针对你需要在长按房产卡片时启动全新应用实例并直接打开预订详情页的需求,我整理了一套完全适配你场景的实现方案(完全避开你不需要的MultipleView方案):

第一步:启用多实例支持

首先必须在Package.appxmanifest中开启多实例支持,记得要添加对应的命名空间(desktop4和iot2),否则配置会无效:

<Package ... xmlns:desktop4="http://schemas.microsoft.com/appx/manifest/desktop/windows10/4" xmlns:iot2="http://schemas.microsoft.com/appx/manifest/iot/windows10/2">
  <Applications>
    <Application Id="App" 
                 Executable="$targetnametoken$.exe" 
                 EntryPoint="YourAppName.App"
                 desktop4:SupportsMultipleInstances="true"
                 iot2:SupportsMultipleInstances="true">
      <!-- 保留你原有的应用配置内容 -->
    </Application>
  </Applications>
</Package>

第二步:运行时启动新实例并传递跳转参数

要实现“跳过角色选择和房产列表直接打开预订详情”,我们可以通过自定义URI协议来传递必要参数(比如选中的角色、房产ID),这样新实例启动时能直接识别并跳转到目标页面。

1. 配置应用的URI协议

先在Package.appxmanifest中添加自定义URI关联,让你的应用可以通过URI被激活:

<Applications>
  <Application ...>
    <Extensions>
      <uap:Extension Category="windows.protocol">
        <uap:Protocol Name="mypropertyapp">
          <uap:DisplayName>Property App Scheme</uap:DisplayName>
        </uap:Protocol>
      </uap:Extension>
    </Extensions>
  </Application>
</Applications>

这里把mypropertyapp改成你自己的专属协议名(比如和你的应用名称相关,避免冲突)。

2. 长按事件中启动新实例的代码

在房产卡片的长按事件处理方法里,构造包含参数的URI,然后调用Launcher.LaunchUriAsync启动全新实例:

private async void PropertyCard_LongPressed(object sender, RoutedEventArgs e)
{
    // 替换成你实际获取的选中角色和房产ID
    string selectedRole = SelectedRole; 
    string targetPropertyId = ((PropertyModel)((FrameworkElement)sender).DataContext).Id;

    // 构造带参数的URI,指定要打开的页面和必要数据
    Uri launchUri = new Uri($"mypropertyapp://openbooking?role={Uri.EscapeDataString(selectedRole)}&propertyId={Uri.EscapeDataString(targetPropertyId)}");

    // 启动新实例
    bool launchSuccess = await Launcher.LaunchUriAsync(launchUri);
    if (!launchSuccess)
    {
        // 这里可以添加启动失败的提示逻辑
        await new MessageDialog("无法启动新实例,请重试").ShowAsync();
    }
}

注意用Uri.EscapeDataString对参数进行编码,避免特殊字符导致解析错误。

3. 新实例中处理URI激活并跳转

修改App.xaml.cs中的OnActivated方法,处理URI激活事件,直接跳转到预订详情页:

protected override void OnActivated(IActivatedEventArgs args)
{
    base.OnActivated(args);

    if (args is ProtocolActivatedEventArgs protocolArgs)
    {
        // 解析URI中的参数
        var queryParams = new WwwFormUrlDecoder(protocolArgs.Uri.Query);
        string role = queryParams.GetFirstValueByName("role");
        string propertyId = queryParams.GetFirstValueByName("propertyId");

        // 初始化根Frame并导航到预订详情页
        Frame rootFrame = Window.Current.Content as Frame;
        if (rootFrame == null)
        {
            rootFrame = new Frame();
            Window.Current.Content = rootFrame;
        }

        // 传递参数并导航,跳过初始页面
        rootFrame.Navigate(typeof(BookingDetailsPage), new BookingNavigationParams 
        { 
            Role = role, 
            PropertyId = propertyId 
        });
        
        Window.Current.Activate();
    }
}

同时,调整OnLaunched方法,确保只有在首次无参数启动时才进入角色选择页面:

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

    if (rootFrame == null)
    {
        rootFrame = new Frame();
        Window.Current.Content = rootFrame;
    }

    // 只有当没有预启动且当前没有内容时,才导航到角色选择页
    if (!e.PrelaunchActivated && rootFrame.Content == null)
    {
        rootFrame.Navigate(typeof(RoleSelectionPage));
    }

    Window.Current.Activate();
}

你可以创建一个BookingNavigationParams类来封装传递的参数,让代码更清晰:

public class BookingNavigationParams
{
    public string Role { get; set; }
    public string PropertyId { get; set; }
}

关键验证点

  • 确认SupportsMultipleInstances配置正确:如果没开这个,新启动会直接激活现有实例,不会创建新窗口
  • 测试URI参数解析:确保特殊字符(比如空格、中文)经过编码后能正确解析
  • 验证启动逻辑:长按卡片时,新实例应该独立打开,并且直接显示对应房产的预订详情,完全跳过角色选择和房产列表

内容的提问来源于stack exchange,提问作者Ankit Dhadse

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 10:08:40