在前面學(xué)習(xí)控件的時(shí)候,我們已經(jīng)見(jiàn)過(guò)了 MessageDialog 了,關(guān)于 Button 還有一個(gè)浮出控件 Flyout 哦。具體是怎樣用呢?接下來(lái)就一起看看。
我們還是延續(xù)前面的那個(gè)示例好了,那么,代碼來(lái)了。
<Button x:Name="btnWhat" Content="這是什么?">
<Button.Flyout>
<Flyout>
<StackPanel>
<TextBlock Width="430" Style="{StaticResource BaseTextBlockStyle}"
Text="噢!你剛剛又踩到地雷了,要撤銷嗎?" Foreground="Red" FontSize="25"/>
<Button Click="btnUndo_Click" Margin="12" Content="撤銷"/>
</StackPanel>
</Flyout>
</Button.Flyout>
</Button>
當(dāng)我們點(diǎn)擊了撤銷按鈕后,當(dāng)然需要 btnWhat 按鈕的 Flyout 消失掉,這個(gè)嘛,也只要 1 行代碼啦。另外這個(gè)踩雷的邏輯這里就不展開(kāi)啦.
private void btnUndo_Click(object sender, RoutedEventArgs e)
{
btnWhat.Flyout.Hide();
}
更為重要的是在于這些在 WP8 上也是通用的,這才是核心所在。既然這一篇教程主要是浮出控件,如果可以借助浮出產(chǎn)生預(yù)覽圖像的效果會(huì)不會(huì)很棒呢?先來(lái)看張運(yùn)行截圖吧。
以下都是代碼啦,什么 Binding 呀之類的都不用管啦。需要注意的地方也就是那些 Height 和 Width 可能需要拿來(lái)調(diào)整一下。
<Page.Resources>
<Flyout x:Key="ResourceFlyoutImage" Placement="Right">
<Image Source="{Binding Path=Source}" MaxHeight="800" MaxWidth="1400" Stretch="Uniform"/>
<Flyout.FlyoutPresenterStyle>
<Style TargetType="FlyoutPresenter">
<Setter Property="MinHeight" Value="900"/>
<Setter Property="MinWidth" Value="1600"/>
<Setter Property="BorderThickness" Value="3"/>
<Setter Property="Background" Value="Wheat"/>
<Setter Property="BorderBrush" Value="Green"/>
<Setter Property="ScrollViewer.ZoomMode" Value="Enabled"/>
</Style>
</Flyout.FlyoutPresenterStyle>
</Flyout>
</Page.Resources>
<Grid>
<StackPanel HorizontalAlignment="Left" Orientation="Vertical">
<Image Source="Assets/14152.jpg" Width="100" Height="100" Margin="12" Tapped="img_Tapped"
FlyoutBase.AttachedFlyout="{StaticResource ResourceFlyoutImage}"
DataContext="{Binding RelativeSource={RelativeSource Mode=Self}}"/>
<Image Source="Assets/14158.jpg" Width="100" Height="100" Margin="12" Tapped="img_Tapped"
FlyoutBase.AttachedFlyout="{StaticResource ResourceFlyoutImage}"
DataContext="{Binding RelativeSource={RelativeSource Mode=Self}}"/>
<Image Source="Assets/14160.jpg" Width="100" Height="100" Margin="12" Tapped="img_Tapped"
FlyoutBase.AttachedFlyout="{StaticResource ResourceFlyoutImage}"
DataContext="{Binding RelativeSource={RelativeSource Mode=Self}}"/>
<Image Source="Assets/14164.jpg" Width="100" Height="100" Margin="12" Tapped="img_Tapped"
FlyoutBase.AttachedFlyout="{StaticResource ResourceFlyoutImage}"
DataContext="{Binding RelativeSource={RelativeSource Mode=Self}}"/>
</StackPanel>
</Grid>
private void img_Tapped(object sender, TappedRoutedEventArgs e)
{
FlyoutBase.ShowAttachedFlyout((FrameworkElement)sender);
}
同樣的,在 WP 上也是可以得哦,一下是做了些修改后的 XAML 代碼啦。正如大家所見(jiàn),我把圖片都縮小了, Placement 也設(shè)置成了 Top,StactPanel 的屬性也做了修改。
<Page.Resources>
<Flyout x:Key="ResourceFlyoutImage" Placement="Top">
<Image Source="{Binding Path=Source}" MaxHeight="600" MaxWidth="400" Stretch="Uniform"/>
<Flyout.FlyoutPresenterStyle>
<Style TargetType="FlyoutPresenter">
<Setter Property="MinHeight" Value="600"/>
<Setter Property="MinWidth" Value="400"/>
<Setter Property="BorderThickness" Value="3"/>
<Setter Property="Background" Value="Wheat"/>
<Setter Property="BorderBrush" Value="Green"/>
<Setter Property="ScrollViewer.ZoomMode" Value="Enabled"/>
</Style>
</Flyout.FlyoutPresenterStyle>
</Flyout>
</Page.Resources>
<Grid>
<StackPanel VerticalAlignment="Bottom" Orientation="Horizontal">
<Image Source="Assets/14152.jpg" Width="72" Height="60" Margin="12" Tapped="img_Tapped"
FlyoutBase.AttachedFlyout="{StaticResource ResourceFlyoutImage}"
DataContext="{Binding RelativeSource={RelativeSource Mode=Self}}"/>
<Image Source="Assets/14158.jpg" Width="72" Height="60" Margin="12" Tapped="img_Tapped"
FlyoutBase.AttachedFlyout="{StaticResource ResourceFlyoutImage}"
DataContext="{Binding RelativeSource={RelativeSource Mode=Self}}"/>
<Image Source="Assets/14160.jpg" Width="72" Height="60" Margin="12" Tapped="img_Tapped"
FlyoutBase.AttachedFlyout="{StaticResource ResourceFlyoutImage}"
DataContext="{Binding RelativeSource={RelativeSource Mode=Self}}"/>
<Image Source="Assets/14164.jpg" Width="72" Height="60" Margin="12" Tapped="img_Tapped"
FlyoutBase.AttachedFlyout="{StaticResource ResourceFlyoutImage}"
DataContext="{Binding RelativeSource={RelativeSource Mode=Self}}"/>
</StackPanel>
</Grid>
看樣子效果還不錯(cuò)嘛。
更多建議: