C# Windows 8应用程序在共享后启动
我正在寻找一种可能性,在通过魅力栏与我的Windows应用程序共享某些内容后,启动我自己的Windows应用程序。 我找到了这个MS示例 但点击共享按钮后,应用程序关闭。 我在按钮单击方法中尝试了以下代码:C# Windows 8应用程序在共享后启动,c#,.net,windows-8,windows-applications,charms-bar,C#,.net,Windows 8,Windows Applications,Charms Bar,我正在寻找一种可能性,在通过魅力栏与我的Windows应用程序共享某些内容后,启动我自己的Windows应用程序。 我找到了这个MS示例 但点击共享按钮后,应用程序关闭。 我在按钮单击方法中尝试了以下代码: var rootFrame = new Frame(); rootFrame.Navigate(typeof(DefaultPage)); Window.Current.Content = rootFrame; Window.Current.Activate(); 但这没有效果。我也尝试过
var rootFrame = new Frame();
rootFrame.Navigate(typeof(DefaultPage));
Window.Current.Content = rootFrame;
Window.Current.Activate();
但这没有效果。我也尝试过使用Application.Start(),但是参数应该是回调,我不知道是哪一个
-------编辑:
我想要以下行为
using System;
using Windows.ApplicationModel;
using Windows.ApplicationModel.Activation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
// The Blank Application template is documented at http://go.microsoft.com/fwlink/?LinkId=234227
namespace AppName
{
/// <summary>
/// Provides application-specific behavior to supplement the default Application class.
/// </summary>
sealed partial class App : Application
{
/// <summary>
/// Initializes the singleton application object. This is the first line of authored code
/// executed, and as such is the logical equivalent of main() or WinMain().
/// </summary>
public App()
{
this.InitializeComponent();
this.Suspending += OnSuspending;
}
/// <summary>
/// Invoked when the application is launched normally by the end user. Other entry points
/// will be used such as when the application is launched to open a specific file.
/// </summary>
/// <param name="e">Details about the launch request and process.</param>
protected override void OnLaunched(LaunchActivatedEventArgs e)
{
#if DEBUG
if (System.Diagnostics.Debugger.IsAttached)
{
this.DebugSettings.EnableFrameRateCounter = true;
}
#endif
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();
// Set the default language
rootFrame.Language = Windows.Globalization.ApplicationLanguages.Languages[0];
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 (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();
}
/// <summary>
/// Invoked when the application is activated as the target of a sharing operation.
/// </summary>
/// <param name="e">Details about the activation request.</param>
protected override void OnShareTargetActivated(Windows.ApplicationModel.Activation.ShareTargetActivatedEventArgs e)
{
var shareTargetPage = new AppName.ShareTargetPage();
shareTargetPage.Activate(e);
Window.Current.Activate();
}
protected override void OnActivated(IActivatedEventArgs args)
{
base.OnActivated(args);
}
}
}
使用系统;
使用Windows.ApplicationModel;
使用Windows.ApplicationModel.Activation;
使用Windows.UI.Xaml;
使用Windows.UI.Xaml.Controls;
使用Windows.UI.Xaml.Navigation;
//空白应用程序模板被记录在http://go.microsoft.com/fwlink/?LinkId=234227
名称空间AppName
{
///
///提供特定于应用程序的行为以补充默认应用程序类。
///
密封部分类应用程序:应用程序
{
///
///初始化singleton应用程序对象。这是编写代码的第一行
///已执行,因此是main()或WinMain()的逻辑等价物。
///
公共应用程序()
{
this.InitializeComponent();
这个.Suspending+=OnSuspending;
}
///
///当最终用户正常启动应用程序时调用。其他入口点
///将在启动应用程序以打开特定文件时使用。
///
///有关启动请求和过程的详细信息。
仅启动受保护的覆盖无效(启动ActivatedEventArgs e)
{
#如果调试
if(System.Diagnostics.Debugger.IsAttached)
{
this.DebugSettings.EnableFrameRateCounter=true;
}
#恩迪夫
Frame rootFrame=Window.Current.Content作为Frame;
//当窗口已经有内容时,不要重复应用程序初始化,
//只需确保窗口处于活动状态
if(rootFrame==null)
{
//创建一个框架作为导航上下文并导航到第一页
rootFrame=新框架();
//设置默认语言
rootFrame.Language=Windows.Globalization.ApplicationLanguages.Languages[0];
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:保存应用程序状态并停止任何后台活动
延迟。完成();
}
///
///当应用程序作为共享操作的目标激活时调用。
///
///有关激活请求的详细信息。
受保护的覆盖无效OnShareTargetActivated(Windows.ApplicationModel.Activation.ShareTargetActivatedEventArgs e)
{
var shareTargetPage=新的AppName.shareTargetPage();
shareTargetPage.Activate(e);
Window.Current.Activate();
}
已激活受保护的覆盖无效(IActivatedEventArgs args)
{
碱基激活(args);
}
}
}
Mainpage.xaml.cs:
using System;
using Windows.UI.Xaml.Controls;
// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238
namespace AppName
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
public void ReceiveUri(Uri sharedWebLink)
{
tbMessages.Text = sharedWebLink.ToString();
}
protected override void OnNavigatedTo(Windows.UI.Xaml.Navigation.NavigationEventArgs e)
{
// It is possible to get in this method after the Share button at the
// sharetargetpage is clicked but at this point I don't know how to
// activate the app
//base.OnNavigatedTo(e);
}
}
}
using Windows.UI.Core;
using AppName.Common;
using System;
using Windows.ApplicationModel.Activation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media.Imaging;
// The Share Target Contract item template is documented at http://go.microsoft.com/fwlink/?LinkId=234241
namespace AppName
{
/// <summary>
/// This page allows other applications to share content through this application.
/// </summary>
public sealed partial class ShareTargetPage : Page
{
private Uri sharedWebLink;
/// <summary>
/// Provides a channel to communicate with Windows about the sharing operation.
/// </summary>
private Windows.ApplicationModel.DataTransfer.ShareTarget.ShareOperation _shareOperation;
private ObservableDictionary defaultViewModel = new ObservableDictionary();
/// <summary>
/// This can be changed to a strongly typed view model.
/// </summary>
public ObservableDictionary DefaultViewModel
{
get { return this.defaultViewModel; }
}
public ShareTargetPage()
{
this.InitializeComponent();
}
/// <summary>
/// Invoked when another application wants to share content through this application.
/// </summary>
/// <param name="e">Activation data used to coordinate the process with Windows.</param>
public async void Activate(ShareTargetActivatedEventArgs e)
{
this._shareOperation = e.ShareOperation;
// Communicate metadata about the shared content through the view model
var shareProperties = this._shareOperation.Data.Properties;
var thumbnailImage = new BitmapImage();
this.DefaultViewModel["Title"] = shareProperties.Title;
this.DefaultViewModel["Description"] = shareProperties.Description;
this.DefaultViewModel["Image"] = thumbnailImage;
this.DefaultViewModel["Sharing"] = false;
this.DefaultViewModel["ShowImage"] = false;
this.DefaultViewModel["Comment"] = String.Empty;
this.DefaultViewModel["Placeholder"] = "Add a comment";
this.DefaultViewModel["SupportsComment"] = true;
Window.Current.Content = this;
Window.Current.Activate();
try
{
this.sharedWebLink = await this._shareOperation.Data.GetWebLinkAsync();
this.DefaultViewModel["URL"] = this.sharedWebLink.ToString();
}
catch (Exception ex)
{
NotifyUserBackgroundThread("Failed GetWebLinkAsync - " + ex.Message, NotifyType.ErrorMessage);
}
// Update the shared content's thumbnail image in the background
if (shareProperties.Thumbnail != null)
{
var stream = await shareProperties.Thumbnail.OpenReadAsync();
thumbnailImage.SetSource(stream);
this.DefaultViewModel["ShowImage"] = true;
}
}
/// <summary>
/// Invoked when the user clicks the Share button.
/// </summary>
/// <param name="sender">Instance of Button used to initiate sharing.</param>
/// <param name="e">Event data describing how the button was clicked.</param>
private void ShareButton_Click(object sender, RoutedEventArgs e)
{
this.DefaultViewModel["Sharing"] = true;
this._shareOperation.ReportStarted();
// TODO: Perform work appropriate to your sharing scenario using
// this._shareOperation.Data, typically with additional information captured
// through custom user interface elements added to this page such as
// this.DefaultViewModel["Comment"]
this._shareOperation.ReportCompleted();
// TRY1: Navigate to MainPage
//Frame rootFrame = Window.Current.Content as Frame;
//if(rootFrame == null)
//{
// rootFrame = new Frame();
// Window.Current.Content = rootFrame;
//}
//if(rootFrame.Content == null)
//{
// rootFrame.Navigate(typeof(MainPage));
//}
// TRY2: Navigate to MainPage
//var p = rootFrame.Content as MainPage;
//p.ReceiveUri(sharedWebLink);
//Window.Current.Activate();
var rootFrame = new Frame();
rootFrame.Navigate(typeof(MainPage));
Window.Current.Content = rootFrame;
Window.Current.Activate();
// TRY3: Start the App
// Application.Start();
// App.Start();
// but I don't know which callback method should be the param of start
}
async private void NotifyUserBackgroundThread(string message, NotifyType type)
{
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
NotifyUser(message, type);
});
}
private void NotifyUser(string strMessage, NotifyType type)
{
switch (type)
{
// Use the status message style.
case NotifyType.StatusMessage:
StatusBlock.Style = Resources["StatusStyle"] as Style;
break;
// Use the error message style.
case NotifyType.ErrorMessage:
StatusBlock.Style = Resources["ErrorStyle"] as Style;
break;
}
StatusBlock.Text = strMessage;
}
public enum NotifyType
{
StatusMessage,
ErrorMessage
};
}
}
使用系统;
使用Windows.UI.Xaml.Controls;
//空白页项模板被记录在http://go.microsoft.com/fwlink/?LinkId=234238
名称空间AppName
{
///
///可以单独使用或在框架内导航到的空页。
///
公共密封部分类主页面:第页
{
公共主页()
{
this.InitializeComponent();
}
public void ReceiveUri(Uri sharedWebLink)
{
tbMessages.Text
protected override void OnShareTargetActivated(ShareTargetActivatedEventArgs args)
{
var rootFrame = new Frame();
// TODO: Load content in frame
Window.Current.Content = rootFrame;
Window.Current.Activate();
}
protected override void OnShareTargetActivated(ShareTargetActivatedEventArgs args)
{
var rootFrame = new Frame();
rootFrame.Navigate(typeof(ShareTargetPage), args);
Window.Current.Content = rootFrame;
Window.Current.Activate();
}
Window.Current.Content = this;
Window.Current.Activate();
protected override void OnNavigatedTo(NavigationEventArgs e)
{
// Calling Activate method here
Activate((ShareTargetActivatedEventArgs)e.Parameter);
}
this.Frame.Navigate(typeof(MainPage));
this._shareOperation.ReportCompleted();