在上一篇中曾留下两个问题,.Net捆绑安装不触发以及路径选择的问题现在都已经解决,这段时间花的最多的地方还是WPF调样式上面,奈何WPF功力不够,暂时还是没有达到自己想要的效果。另外ViewModel做了些调整,状态更加分明。安装效果是仿照搜狗输入法做的。先上效果图。
以上这只是四个基本的页面,更细化一些可以分出卸载的进度页面,卸载的完成页面,对修复同理,还有安装失败,用户取消提示页面,再就是能检测新版本并覆盖安装,这些都可以根据状态去添加页面。下面简单的说下思路和新的问题点。
关键流程
安装包运行之后,首先触发的重要的事件之一就是 DetectPackageComplete,Bootstrapper按照Bundle.wxs Chain里面Package的顺序来检测当前电脑是否安装了其中的程序。
<Chain DisableRollback='yes'> <PackageGroupRef Id="Netfx4Full" /> <MsiPackage Id="HeartBeats" SourceFile="D:\SetUp\Main\TestWix\bin\Debug\zh-cn\WPFDemo.msi" Compressed="yes" DisplayInternalUI="no" > <MsiProperty Name="INSTALLFOLDER" Value="[InstallFolder]"/> MsiPackage> Chain>
像这样,首先会判断是否安装了.Net4.0,但是在WPF程序里面我们需要判断的还是HeartBeats这个安装包。·
protected void DetectPackageComplete(object sender, DetectPackageCompleteEventArgs e) { PackageId = e.PackageId; //对应的是MsiPackage Id="HeartBeats" //MessageBox.Show(e.PackageId + e.State); if (e.PackageId.Equals("HeartBeats", StringComparison.Ordinal)) { State = e.State == PackageState.Present ? InstallState.Present : InstallState.NotPresent; //State = InstallState.NotPresent; } }
如果这个安装包的State等于 PackageState.Present 那说明当前电脑已经安装过了,那就要出现卸载和修复的界面,反之就是出现安装的界面(因此也可以控制用户不能直接卸载,可以让用户提交一些数据之后再卸载)。然后会执行PlanBegin->PlanComplete->
ApplyBegin->ApplyComplete. 从字面意思理解就是安装准备到执行完毕的过程。InstallState的值就是在这些事件中发生变化,这也是从进度条页面切换到完成页面的顺序。InstallState 为Applied就是安装完成了。
public enum InstallState { Initializing, Present, NotPresent, Applying, Cancelled, Applied, Failed, }
其他的一些细节在前文里面有讲,这里就不再赘述。
安装界面布局
这里的布局主要是四个Grid根据状态不断的切换。当然你可以添加更多的状态也展示不同的页面。
<Grid Grid.Row="1" Background="White" Visibility="{Binding InstallEnabled,COnverter={StaticResource BooleanToVisibilityConverter}}" > <Grid.RowDefinitions> <RowDefinition Height="2*" /> <RowDefinition Height="*" /> Grid.RowDefinitions> <Rectangle MouseLeftButtonDown="Background_MouseLeftButtonDown" /> <StackPanel Grid.Row="0" VerticalAlignment="Stretch" Style="{StaticResource InstallGrid}" > <StackPanel.Background> <LinearGradientBrush StartPoint="0,0" EndPoint="0,1"> <GradientStop Color="White" Offset="0" /> <GradientStop Color="AliceBlue" Offset="1" /> LinearGradientBrush> StackPanel.Background> <TextBlock HorizontalAlignment="Center" Padding="10" FontSize="40" Foreground="#33CCFF" IsHitTestVisible="False" Text="{Binding SoftName}" >TextBlock> <Button Height="40" HorizontalAlignment="Center" IsEnabled="{Binding IsAgree,UpdateSourceTrigger=PropertyChanged}" Width="200" Style="{StaticResource BuleBt}" Command="{Binding InstallCommand}" >立即安装Button> <TextBlock Foreground="Gray" Padding="5 10 0 0" VerticalAlignment="Center" HorizontalAlignment="Center" TextDecorations="{x:Null}"> <Hyperlink NavigateUri="http://www.cnblogs.com/stoneniqiu/" RequestNavigate="Hyperlink_OpenGuid" > <Label Content="安装指南" VerticalContentAlignment="Bottom" FontSize="13" Margin="0" Padding="0" Foreground="#666" >Label> Hyperlink> TextBlock> StackPanel> <Border Grid.Row="1" HorizontalAlignment="Left" Width="580" Height="110" VerticalAlignment="Top" BorderBrush="Gainsboro" BorderThickness="0 0 0 0"> <Grid Style="{StaticResource DirGrid}" > <Grid.RowDefinitions> <RowDefinition /> <RowDefinition /> <RowDefinition /> Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="*">ColumnDefinition> <ColumnDefinition Width="3*" >ColumnDefinition> Grid.ColumnDefinitions> <Label Grid.Row="0" Grid.Column="0" Content="安装位置:" HorizontalAlignment="Right" VerticalContentAlignment="Center" >Label> <TextBox Grid.Column="1" Grid.Row="0" Width="280" Height="25" Background="White" Margin="10 5" HorizontalAlignment="Left" Text="{Binding InstallFollder,UpdateSourceTrigger=PropertyChanged}" >TextBox> <Button Grid.Column="1" Grid.Row="0" Width="50" Height="25" Style="{StaticResource BuleBt}" Margin="0 0 90 0" Name="SelectFile" Click="SelectFile_OnClick" FontSize="14" HorizontalAlignment="Right" >浏览Button> <CheckBox Grid.Row="1" IsChecked="{Binding IsAgree,UpdateSourceTrigger=PropertyChanged}" Grid.Column="0" Grid.ColumnSpan="2" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="10 0 0 0" > <TextBlock Foreground="Gray" Padding="5 0 0 0" VerticalAlignment="Center" TextDecorations="{x:Null}"> <Label Content="我已经阅读并最终接受:" VerticalContentAlignment="Bottom" FontSize="12" Margin="0" Padding="0" Foreground="Gray" >Label> <Hyperlink NavigateUri="http://www.cnblogs.com/stoneniqiu/" RequestNavigate="Hyperlink_OnRequestNavigate" > <Label Content="用户协议" VerticalContentAlignment="Bottom" FontSize="12" Margin="0" Padding="0" Foreground="DodgerBlue" >Label> Hyperlink> TextBlock> CheckBox> <Label Name="InstalLabel" Grid.Column="0" Grid.ColumnSpan="2" Content="{Binding SpaceInfo,UpdateSourceTrigger=PropertyChanged}" Foreground="Gray" HorizontalAlignment="Center" Grid.Row="2" Margin="5 0 0 0" /> Grid> Border> Grid> <Grid Grid.Row="1" Visibility="{Binding UninstallEnabled,COnverter={StaticResource BooleanToVisibilityConverter}}"> <Grid.Background> <LinearGradientBrush StartPoint="0,0" EndPoint="0,1"> <GradientStop Color="White" Offset="0" /> <GradientStop Color="WhiteSmoke" Offset="1" /> LinearGradientBrush> Grid.Background> <Grid.ColumnDefinitions> <ColumnDefinition Width="*" /> <ColumnDefinition Width="2*"/> <ColumnDefinition Width="*"/> Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="35" /> <RowDefinition Height="45"/> <RowDefinition /> <RowDefinition Height="50" /> Grid.RowDefinitions> <Label Grid.Row="1" Grid.Column="1" Content="请选择:" HorizontalAlignment="Left" VerticalAlignment="Top" Foreground="Green" FontSize="25" >Label> <StackPanel Grid.Row="2" Grid.Column="1"> <Button Content="卸载" IsDefault="True" HorizontalAlignment="Left" Margin="10 10" Height="35" Width="120" Style="{StaticResource BuleBt}" Background="Tomato" Command="{Binding UninstallCommand}" >Button> <Button Content="修复" IsDefault="True" HorizontalAlignment="Left" Margin="10 10" Height="35" Width="120" Style="{StaticResource BuleBt}" Command="{Binding RepairCommand}" >Button> StackPanel> <TextBlock Grid.Row="3" Foreground="Gray" Grid.Column="0" Padding="5 0 0 0" VerticalAlignment="Center" Grid.ColumnSpan="2" TextDecorations="{x:Null}"> <Label Content="登陆官网了解更多:" VerticalContentAlignment="Bottom" FontSize="12" Margin="0" Padding="0" Foreground="Gray" >Label> <Hyperlink NavigateUri="{Binding WebSite }" RequestNavigate="Hyperlink_OnRequestNavigate" > <Label Content="{Binding WebSite}" VerticalContentAlignment="Bottom" FontSize="12" Margin="0" Padding="0" Foreground="Gray" >Label> Hyperlink> TextBlock> Grid>