Windows Phone 8 SDK中没有FlipView控件,实现类似功能的替代方法是什么?
答案 0 :(得分:3)
这里有一些选择。如果您只想显示固定数量的“页面”,则可以使用Panorama应用。如果要显示大量页面,则需要使用pivot控件。
如果您必须重新实现FlipView,可以在应用程序栏中输入“left”和“right”按钮。您需要使用自定义控件构建自己的内容区域。如果您要进行“酷”基于手势的导航,则需要获取toolkit。
答案 1 :(得分:1)
这是WP8的自定义FlipView控件,如WINRT FlipView Control ......
步骤1:添加新的Usercontrol并将其命名为" FlipView.xaml"
第2步:在" FlipView.xaml"
中添加以下xaml <Grid x:Name="LayoutRoot" Background="{StaticResource PhoneChromeBrush}">
<ContentPresenter Name="contentPresenter"/>
<Button BorderThickness="0" Name="leftButton" FontSize="70" Margin="-25" HorizontalAlignment="Left" VerticalAlignment="Center" Content="<" Click="Button_Click"/>
<Button BorderThickness="0" Name="rightButton" FontSize="70" Margin="-25" HorizontalAlignment="Right" VerticalAlignment="Center" Content=">" Click="Button_Click_1"/>
</Grid>
步骤3:在&#34; FlipView.cs&#34;
中添加以下代码public partial class FlipView : UserControl
{
public FlipView()
{
InitializeComponent();
Datasource = new List<object>();
SelectedIndex = 0;
}
private IList Datasource;
public static readonly DependencyProperty ItemTemplateProperty =
DependencyProperty.Register("ItemTemplate", typeof(DataTemplate), typeof(FlipView), new PropertyMetadata(default(DataTemplate)));
public DataTemplate ItemTemplate
{
get { return (DataTemplate)GetValue(ItemTemplateProperty); }
set
{
SetValue(ItemTemplateProperty, value);
contentPresenter.ContentTemplate = value;
contentPresenter.Content = SelectedItem;
}
}
public static readonly DependencyProperty ItemsSourceProperty =
DependencyProperty.Register("ItemsSource", typeof(IList), typeof(FlipView), new PropertyMetadata(default(IList)));
public IList ItemsSource
{
get { return (IList)GetValue(ItemsSourceProperty); }
set
{
SetValue(ItemsSourceProperty, value);
Datasource = value;
SelectedIndex = SelectedIndex;
}
}
public static readonly DependencyProperty SelectedIndexProperty =
DependencyProperty.Register("SelectedIndex", typeof(int), typeof(FlipView), new PropertyMetadata(default(int)));
public int SelectedIndex
{
get { return (int)GetValue(SelectedIndexProperty); }
set
{
SetValue(SelectedIndexProperty, value);
rightButton.Visibility = leftButton.Visibility = Visibility.Visible;
if (SelectedIndex == 0)
{
leftButton.Visibility = Visibility.Collapsed;
}
if (SelectedIndex + 1 == Datasource.Count)
{
rightButton.Visibility = Visibility.Collapsed;
SelectedItem = Datasource[SelectedIndex];
}
if (Datasource.Count > SelectedIndex + 1)
{
SelectedItem = Datasource[SelectedIndex];
}
}
}
public static readonly DependencyProperty SelectedItemProperty =
DependencyProperty.Register("SelectedItem", typeof(object), typeof(FlipView), new PropertyMetadata(default(object)));
public object SelectedItem
{
get { return (object)GetValue(SelectedItemProperty); }
set
{
SetValue(SelectedItemProperty, value);
contentPresenter.Content = SelectedItem;
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
SelectedIndex--;
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
SelectedIndex++;
}
}
步骤4:现在在主页上添加命名空间以使用flipview Usercontrol
实施例: 的xmlns:FlipViewControl =&#34; CLR-名称空间:ImageFlip&#34; (注意:根据您的解决方案名称而有所不同)。
步骤5:使用命名空间,添加flipview控件,如下所示..
<Grid x:Name="LayoutRoot" Background="Transparent">
<FlipViewControl:FlipView Name="imgViewer">
<FlipViewControl:FlipView.ItemTemplate>
<DataTemplate>
<Image Source="{Binding}" Stretch="Fill"/>
</DataTemplate>
</FlipViewControl:FlipView.ItemTemplate>
</FlipViewControl:FlipView>
</Grid>
步骤6:在mainpage.cs中添加以下代码
// Constructor
public MainPage()
{
InitializeComponent();
// Sample code to localize the ApplicationBar
//BuildLocalizedApplicationBar();
imgViewer.ItemsSource = new List<string> { "/Images/1.jpg", "/Images/2.jpg", "/Images/3.jpg" };
}
希望这会有所帮助。
谢谢
答案 2 :(得分:1)
我使用了Windows Phone Toolkit中的FlipView
版本。
如果您启用了NuGet扩展,那么获取Toolkit非常简单:在Solution Exporer中右键单击您的项目 - &gt;管理NuGet包 - &gt;确保选择在线(在左栏中) - &gt;在搜索字段中键入(右列)“工具包” - &gt;单击相应包上的“安装”按钮。
使用后面代码中的FlipView
非常简单:
Microsoft.Phone.Controls.FlipView flipView = new Microsoft.Phone.Controls.FlipView();
flipView.ItemSource = myItemSource;
flipView.ItemTemplate = myItemTemplate;
我更喜欢使用这种方法,因为此FrameworkElement可以很好地响应滑动手势。