使用启动应用程序中的app-protocol打开新的UWP应用程序

时间:2018-01-24 15:01:13

标签: c# uwp windows-10-universal windowsiot windows-iot-core-10

我可以在Windows 10和通用Windows应用程序中使用app-protocol功能启动应用程序。 首先,我在应用程序B中的 Package.appxmanifest 文件中声明一个协议,然后从调用应用程序A的主应用程序中运行,运行此代码以运行应用程序B:

class Base
  class Child
    def im
      "from base child"
    end

    def common
      "shared by all subclasses"
    end
  end

  def self.inherited subclass
    # define a new Child class that inherits from Base::Child
    subclass.const_set "Child", Class.new(self::Child)
  end

  def initialize
    @child = self.class::Child.new
  end

  def test
    puts @child.im
    puts @child.common
  end
end

class A < Base
   # now this is opening Child, but not Base::Child! Rather A::Child < Base::Child
   class Child
     def im
       "from a child"
     end
   end
end

class B < Base
  # same here, opening a subclass of Base::Child so we can make changes but still share behavior with Base::Child
  class Child
    def im
      "from b child"
    end
  end
end

A.new.test
# from a child
# shared by all subclass
B.new.test
# from b child
# shared by all subclasses

但我面临一个问题,当主要应用程序启动时,我不能在应用程序B中午餐,我该怎么做?

2 个答案:

答案 0 :(得分:1)

问题是声明协议本身不足以让应用程序响应它。您还需要在应用B中实现协议激活:

public partial class App
{
   protected override void OnActivated(IActivatedEventArgs args)
  {
      if (args.Kind == ActivationKind.Protocol)
      {
            Frame rootFrame = Window.Current.Content as Frame;

            if (rootFrame == null)
            {
                rootFrame = new Frame();
                rootFrame.NavigationFailed += OnNavigationFailed;
                Window.Current.Content = rootFrame;
            }
            if (rootFrame.Content == null)
            {
                rootFrame.Navigate(typeof(MainPage), e.Arguments);
            }
            Window.Current.Activate();
      }
   }
}

如果应用尚未启动,您需要在OnActivated中执行的初始化可能类似于OnLaunched。如果应用程序已经运行,您不需要做任何特殊的事情,它只会到达前台。如果它未运行,您必须创建主Frame,初始化Window.Current.Content,然后执行Window.Current.Activate()以激活该应用。

答案 1 :(得分:0)