在上一篇《迁移桌面程序到MS Store(7)——APPX + Service》中,我们提到将desktop application拆分成UI Client+Service两部分。其中UI Client可以通过Desktop Bridge技术Pacakage成APPX,上传到MS Store以供下载,而Service则仍以传统的desktop application安装包形式提供。这样势必造成用户安装时的割裂感。本篇将就这个问题进行一些讨论。

首先我们参照上图的架构创建Sample Solution,其中包括充当UI的WPFClient,升级到.NET Standard的DownloadLib,以及打包用的UWPClientPackaging工程(请先暂时忽略UWPClient工程)。

WPFClient只是一个空的Window,仅仅在Window Load的时候,询问用户是否下载文件。

        private async void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
txtLog.AppendText($"Ask user to download file.\n");
var result = MessageBox.Show("We need to download file.", "Download", MessageBoxButton.YesNo);
if (result == MessageBoxResult.Yes)
{
txtLog.AppendText($"Start downloading.\n");
this.progressBar.Visibility = Visibility.Visible;
var downloader = new SimpleDownloader();
var stream = await downloader.RequestHttpContentAsync(
ConfigurationManager.AppSettings["uri"],
ConfigurationManager.AppSettings["username"],
ConfigurationManager.AppSettings["password"]); this.progressBar.Visibility = Visibility.Collapsed;
txtLog.AppendText($"Done.\n"); var path = SaveFile(stream);
txtLog.AppendText($"File path is {path}.\n"); Process.Start(path);
txtLog.AppendText($"Start process {path}.\n");
}
}

这里需要注意的是,代码中的uri,username和password均写在配置文件App.config中,调试时记得填写真实的值。

  <appSettings>
<add key="uri" value=""/>
<add key="username" value=""/>
<add key="password" value=""/>
</appSettings>

DownloadLib工程在这个例子中充当了Class Library的角色,考虑到会被WPFClient和UWPClient同时调用,DownloadLib的项目类型是.NET Standard。该工程的代码也很简单,通过传入的uri,username和password进行http请求下载文件,以Stream的形式返回结果。

        public async Task<Stream> RequestHttpContentAsync(string uriString, string userName, string password)
{
using (HttpClient client = new HttpClient())
{
client.BaseAddress = new Uri(uriString);
client.DefaultRequestHeaders.Accept.Clear();
var authorization = Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes($"{userName}:{password}"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", authorization);
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); HttpResponseMessage response = await client.GetAsync(uriString);
if (response.IsSuccessStatusCode)
{
HttpContent content = response.Content;
var contentStream = await content.ReadAsStreamAsync();
return contentStream;
}
else
{
throw new FileNotFoundException();
}
}
}

假设我们这里下载的文件是一个.msi的安装文件,这个安装文件即是架构图中Service部分的安装包。在完成下载后,在WPFClient中将Stream保存成文件,然后通过Process.Start(path);运行,接下来就是.msi文件的安装流程了。在安装结束后,整个application就可以正常使用了。

        private string SaveFile(Stream stream)
{
var filePath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "installFile.msi");
using (var fileStream = new FileStream(filePath, FileMode.Create))
{
byte[] buffer = new byte[];
int bytesRead;
do
{
bytesRead = stream.Read(buffer, , );
fileStream.Write(buffer, , bytesRead);
} while (bytesRead > ); } return filePath;
}

WPFClient最终是通过UWPClientPackaging工程打包成为APPX,实际对File和Process的操作都是标准的WPF代码。不存在权限的问题。当然如果当前用户没有admin权限,不被允许安装任何软件,这就超出了我们讨论的范围。
接下来我们来看纯UWPClient的情况。UWPClient工程同样添加了对DownloadLib的引用,界面也基本一致。稍有不同之处在于不能使用System.IO.File对象,而是要通过StorageFolder来保存文件。同样也不能够使用Process.Start()方法,而是通过Launcher对象来打开保存文件所在的文件夹。某软出于安全角度,不允许Launcher对象运行exe,msi等类型的可执行文件。所以只能打开文件夹让用户手动点击安装。
https://docs.microsoft.com/en-us/uwp/api/windows.system.launcher.launchfileasync
This API also imposes several restrictions on what types of files it can launch. Many file types that contain executable code, for example .exe, .msi, and .js files, are blocked from launching. This restriction protects users from potentially malicious files that could modify the system.

        private async void MainPage_Loaded(object sender, RoutedEventArgs e)
{
txtLog.Text += $"Ask user to download file.\n";
var dialog = new MessageDialog("Do you want to download installation file?");
dialog.Commands.Add(new UICommand { Label = "Ok", Id = });
dialog.Commands.Add(new UICommand { Label = "Cancel", Id = });
var res = await dialog.ShowAsync(); if ((int)res.Id == )
{
txtLog.Text += $"Start downloading.\n";
this.progressRing.IsActive = true;
var downloader = new SimpleDownloader();
var stream = await downloader.RequestHttpContentAsync(
"",
"",
""); this.progressRing.IsActive = false;
var file = await SaveStorageFile(stream);
var result = await Launcher.LaunchFolderAsync(ApplicationData.Current.LocalFolder);
txtLog.Text += $"Done.\n ";
}
}

本篇讨论了如何在APPX中下载文件并运行,使用户仅需要在MS Store中进行一次下载即可完成整个application的安装。实际使用中更适合通过Desktop Bridge打包的desktop application。而对纯UWP的客户端并不友好。对纯UWP客户端的处理我们在下一篇中做更进一步讨论。
GitHub:
https://github.com/manupstairs/AppxDownloadWin32Component

迁移桌面程序到MS Store(8)——通过APPX下载Win32Component的更多相关文章

  1. 迁移桌面程序到MS Store(5)——.NET Standard

    接下来的几篇,我想讨论下迁移桌面程序到MS Store,可以采用的比较常见.通用性比较强的实施步骤和分层架构. 通常商业项目一般都是不断的迭代,不太可能突然停止更新现有的桌面版本,然后花很长时间从头来 ...

  2. 迁移桌面程序到MS Store(1)——通过Visual Studio创建Packaging工程

    之前跑去做了一年多的iOS开发,被XCode恶心得不行.做人呢,最重要的是开心.所以我就炒了公司鱿鱼,挪了个窝回头去做Windows开发了.        UWP什么的很久没有正儿八经写了,国内的需求 ...

  3. 迁移桌面程序到MS Store(9)——APPX With Desktop Extension

    在<迁移桌面程序到MS Store(8)——通过APPX下载Win32Component>中我们讨论了通过APPX来下载Service部分的安装包.但是纯UWP的客户端并不能自动运行下载的 ...

  4. 迁移桌面程序到MS Store(10)——在Windows S Mode运行

    首先简单介绍Windows 10 S Mode,Windows在该模式下,只能跑MS Store里的软件,不能通过其他方式安装.好处是安全有保障,杜绝一切国产流氓软件.就像iOS一样,APP进商店都需 ...

  5. 迁移桌面程序到MS Store(12)——WPF使用UWP InkToolbar和InkCanvas

    我们在<迁移桌面程序到MS Store(4)——桌面程序调用Win10 API>提到了对Win10 API的调用,但仍存在无法在WPF中使用UWP控件的问题,虽然都是XAML控件,但却是两 ...

  6. 迁移桌面程序到MS Store(13)——动态检查Win10 API是否可用

    假设我们现有一个WPF程序,需要支持1903以前的Windows 10版本.同时在1903以后的版本上,额外多出一个Ink的功能.那么我们就可以通过ApiInformation.IsApiContra ...

  7. 迁移桌面程序到MS Store(14)——APPX嵌入WCF Service以Admin权限运行

    Windows10 1809版本开始,微软又对UWP开放了新的Capability:AllowElevation. 通过这个新的Capability,UWP APP能够在运行时向用户请求Admin权限 ...

  8. 迁移桌面程序到MS Store(2)——Desktop App Converter

    迁移传统桌面程序到MS Store的另一种方式是使用Desktop App Converter工具.虽然本篇标题包含了Desktop App Converter(以下简称DAC),实际上我是来劝你别用 ...

  9. 迁移桌面程序到MS Store(3)——开机自启动

    迁移桌面程序的时候,有可能你会遇到这么个需求——开机自启动.Windows传统桌面程序的传统陋习.不论什么奇葩软件都想要开机自启动,默认就给你打开,一开机哐哐哐什么雷,什么企鹅都蹦出来,也不管你用不用 ...

随机推荐

  1. java HttpServletRequest 重复流读取

    在用reset接口的时候,常常会使用request.getInputStream()方法,但是流只能读取一次,一旦想要加上一个过滤器用来检测用户请求的数据时就会出现异常.   在过滤器中通过流读取出用 ...

  2. cdlinux

    xset q xset s 6000 xset -dpms ntpdate time.nist.gov date

  3. Codeforces 727C Guess the Array

    题目传送门 长度为\(n\)的数组,询问\(n\)次来求出数组中每一个数的值 我们可以先询问三次 \(a[1]+a[2]\) \(a[1]+a[3]\) \(a[2]+a[3]\) 然后根据这三次询问 ...

  4. UISearchBar的应用

    当你在seachBar中输入字母之前的时候,只是用鼠标选中searchBar的时候,如图 终端输出截图如下:(这个时候调用先shouldBeginEditing,之后调用didBeginEditing ...

  5. css flew 布局 解决父元素高度不固定,子级居中。

    给父级添加 display: flex; justify-content: flex-start; align-items: center; 子级里的内容永远居中

  6. Springboot(二)-application.yml默认的配置项以及读取自定义配置

    写在前面 ===== spring-boot 版本:2.0.0.RELEASE ===== 读取自定义配置 1.配置文件:sys.properties supply.place=云南 supply.c ...

  7. LeetCode(7)Reverse Integer

    题目: Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 分析: ...

  8. HDU 3790 (最短路 + 花费)

    题意: 给你n个点,m条无向边,每条边都有长度d和花费p,给你起点s终点t,要求输出起点到终点的最短距离及其花费,如果最短距离有多条路线,则输出花费最少的. #include<bits/stdc ...

  9. 转:Ubuntu下ibus-sunpinyin的安装及翻页快捷键设置!

    在windows下,好多人都已经习惯了使用搜狗拼音,到ubuntu下,忽然没有极为顺手的输入法,实为郁闷,但是确实还没有for linux版本的搜狗使用,这是搜狗的商业策略,我们无法掌控,但是,如果你 ...

  10. 【原】缓存之 HttpRuntime.Cache

    1.HttpRuntime.Cache HttpRuntime.Cache 相当于就是一个缓存具体实现类,这个类虽然被放在了 System.Web 命名空间下了.但是非 Web 应用也是可以拿来用的. ...