将FileOpenPicker与Silverlight一起使用

时间:2014-05-04 12:20:47

标签: c# silverlight xaml windows-phone-8 windows-phone-8.1

Windows Phone 8.1支持文件打开和文件保存选择器。我想使用一个文件打开选择器和一个从WP 8转换为WP 8.1(Silverlight)的项目。

我可以按如下方式打开FileOpenPicker:

FileOpenPicker picker = new FileOpenPicker();
picker.FileTypeFilter.Add(".txt");
picker.PickSingleFileAndContinue(); 

现在,我发现的所有示例都使用新的通用Windows运行时,其中生成的文件在App.xaml.cs中按如下方式捕获:

protected override void OnActivated(IActivatedEventArgs e) {     
    ContinuationActivatedEventArgs = e as IContinuationActivatedEventArgs; 
    if (ContinuationEventArgsChanged != null) 
    { 
        // Handle file here
    } 
} 

问题是转换后的Silverlight应用程序没有实现此方法。相反,我从Silverlight应用程序的另一个示例(http://msdn.microsoft.com/en-us/library/dn655125%28v=vs.105%29.aspx)派生了原则构思:

private void Application_Activated(object sender, ActivatedEventArgs e)
{
    var eventArgs = e as IContinuationActivatedEventArgs;
    if (eventArgs != null)
    {
        // Handle file here
    }
}

但这不起作用(例如eventArgs总是NULL)。

这里有另一个例子:http://msdn.microsoft.com/en-us/library/windowsphone/develop/dn642086%28v=vs.105%29.aspx。这在app.xaml.cs中使用以下方法:

private void Application_ContractActivated(object sender, IActivatedEventArgs e)
{
        var filePickerContinuationArgs = e as FileOpenPickerContinuationEventArgs;
        if (filePickerContinuationArgs != null)
        {
        // Handle file here
        }
}

但是我的应用程序永远不会调用此方法。

有人提示/想法如何让FileOpenPicker与Silverlight WP8.1应用程序一起使用吗?

此致

1 个答案:

答案 0 :(得分:2)

似乎需要手动将事件处理程序添加到Microsoft.Phone.Shell.PhoneApplicationService.Current.ContractActivated:

Microsoft.Phone.Shell.PhoneApplicationService.Current.ContractActivated +=  Application_ContractActivated;

private void Application_ContractActivated(object sender, IActivatedEventArgs e)
{
        var filePickerContinuationArgs = e as FileOpenPickerContinuationEventArgs;
        if (filePickerContinuationArgs != null)
        {
        // Handle file here
        }
}