Page 20 - MSDN Magazine, May 2018
P. 20

Figure 6 Overriding OnActivated for Command-Line Activation
protected override void OnActivated(IActivatedEventArgs args) {
switch (args.Kind) {
case ActivationKind.CommandLineLaunch: CommandLineActivatedEventArgs cmdLineArgs =
args as CommandLineActivatedEventArgs; CommandLineActivationOperation operation = cmdLineArgs.Operation; string activationPath = operation.CurrentDirectoryPath;
Frame rootFrame = Window.Current.Content as Frame; if (rootFrame == null)
{
rootFrame = new Frame();
Window.Current.Content = rootFrame; }
rootFrame.Navigate(typeof(MainPage), activationPath); Window.Current.Activate();
break;
} }
Figure 7 Building a File-System Tree from the Current Work- ing Directory
protected async override void OnNavigatedTo(NavigationEventArgs e) {
string activationPath = e.Parameter as string; argumentsText.Text = activationPath; fileTreeView.RootNodes.Clear();
try {
StorageFolder folder =
await StorageFolder.GetFolderFromPathAsync(activationPath);
if (folder != null) {
TreeViewNode rootNode = new TreeViewNode() { Content = folder.Name }; IReadOnlyList<StorageFolder> folders = await folder.GetFoldersAsync(); GetDirectories(folders, rootNode); fileTreeView.RootNodes.Add(rootNode);
} }
catch (Exception ex) {
Debug.WriteLine(ex.Message); }
}
New Capability
The second way that more file-system access is being provided is via a new restricted capability. To use this, you must declare the restrictedcapabilities XML namespace at the top of your app mani- fest, and include broadFileSystemAccess in your <Capabilities> list:
xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/ windows10/restrictedcapabilities"
IgnorableNamespaces="uap mp uap5 rescap">
...<Capabilities>
<rescap:Capability Name="broadFileSystemAccess" />
</Capabilities>
If you declare any restricted capability, this triggers additional scru- tiny at the time you submit your package to the Store for publication. If the app is granted this capability, it will have the same access to the file-system as the user running the app. Not just from the current work- ing directory but everywhere that the user has access. You don’t need an AppExecutionAlias if you have this capability. Because this is such a powerful feature, Microsoft will grant the capability only if the app developer provides compelling reasons for the request, a description of how this will be used, and an explanation of how this benefits the user.
Figure 8 New File System Page in Settings
If you declare the broadFileSystemAccess capability, you don’t need to declare any of the more narrowly scoped file-system capabilities (Documents, Pictures or Videos); indeed, an app must not declare both broadFileSystemAccess and any of the other three file-system capabilities.
Even after the app has been granted the capability, there’s also a runtime check, because this constitutes a privacy concern for the user. Just like other privacy issues, the app will trigger a user-consent prompt on first use. If the user chooses to deny permission, the app must be resilient to this. The user can also change her mind at any time, by going to the relevant File system page under the Privacy list in Settings, as shown in Figure 8.
Note that to take advantage of both the current working directory access and the broadFileSystemAccess permissions, your code must use the WinRT Windows.Storage APIs for file handling.
Wrapping Up
One of the long-term strategies with the UWP is to close the gaps with earlier app technologies—especially Win32—so that the UWP is a viable option for more and more different app types over time. With the introduction of support for true multi-instancing, con- sole UWP apps, and broader file-system access, three more large steps have been taken on this journey. Sample code is available at bit.ly/2GtzM3T, and you’ll find the Visual Studio project templates at bit.ly/2HApmii and bit.ly/2FEIAXu. n
Andrew whitechApel is a program manager in the Microsoft Windows division, responsible for the app activation workflow for the Universal Windows Platform.
thAnks to the following technical experts for reviewing this article: Jason Holmes, Tim Kurtzman, Anis Mohammed Khaja Mohideen
16 msdn magazine
Universal Windows Platform


































































































   18   19   20   21   22