c# - Windows 10 IoT Xaml动态添加图片到flipview

标签 c# xaml win-universal-app raspberry-pi2 windows-10-iot-core

我的项目中包含一个包含图像的文件夹。这些图像应该加载到 flipview-Control 中,它会显示它们并在 3 秒后显示下一张。切换已经有效,但我在显示图像时遇到问题。

public MainPage()
{
    this.InitializeComponent();
    String path = Directory.GetCurrentDirectory() + @"\Imports";
    foreach (String imgurl in Directory.GetFiles(path))
    {
        String filename = Path.GetFileName(imgurl);
        Uri uri = new Uri("ms-appx:///" + filename);
        var image = new Image
        {
            Source = new BitmapImage(uri)
        };
        // fv is my flipview
        fv.Items.Add(image);
    }

    timer = new DispatcherTimer()
    {
        Interval = TimeSpan.FromSeconds(3)
    };
    timer.Tick += ChangeImage;
    timer.Start();
}

private void ChangeImage(object sender, object o)
{
    var totalItems = fv.Items.Count;
    var newItemIndex = (fv.SelectedIndex + 1) % totalItems;
    fv.SelectedIndex = newItemIndex;
}

我的 XAML 代码如下所示:

<FlipView x:Name="fv" HorizontalAlignment="Left" VerticalAlignment="Top" Width="1280" Height="720" SelectionChanged="flipView_SelectionChanged"></flipview>

正如我所说,滑动有效,但图像未显示。我错过了什么?还有,

fv.Items.Count();

总是告诉我,它包含的图像数量是我在“导入”文件夹中的两倍。为什么?

编辑:

private readonly DispatcherTimer timer;

    public MainPage()
    {
        this.InitializeComponent();

        String path = Directory.GetCurrentDirectory() + @"\Imports";
        fv.ItemsSource = Directory.GetFiles(path);

        timer = new DispatcherTimer()
        {
            Interval = TimeSpan.FromSeconds(3)
        };
        timer.Tick += ChangeImage;
        timer.Start();
    }

    private void ChangeImage(object sender, object o)
    {
        var totalItems = fv.Items.Count;
        var newItemIndex = (fv.SelectedIndex + 1) % totalItems;
        fv.SelectedIndex = newItemIndex;

    }

最佳答案

您必须添加一个 ItemTemplate 才能显示图像:

<FlipView x:Name="fv" HorizontalAlignment="Left" VerticalAlignment="Top" Width="1280" Height="720" SelectionChanged="flipView_SelectionChanged">
    <FlipView.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Image Source="{Binding PathToImage}" Stretch="UniformToFill" />
            </Grid>
        </DataTemplate>
    </FlipView.ItemTemplate>
</FlipView>

Source 只需要一个图像路径即可工作。这意味着您可以在代码隐藏中执行此操作:

String path = Directory.GetCurrentDirectory() + @"\Imports";
fv.ItemsSource = Directory.GetFiles(path);

对于 fv.Items.Count(); 的意外返回值:你有集合中项目的转储吗?类似

的东西
foreach(var item in fv.Items.Count()) System.Diagnostics.Debug.WriteLine(item);

或者,您可以将所有图像添加到新列表<>,并将该列表设置为 ItemsSource

String path = Directory.GetCurrentDirectory() + @"\Imports";
var newSource = new List<Image>();
foreach (String imgurl in Directory.GetFiles(path))
{
    String filename = Path.GetFileName(imgurl);
    Uri uri = new Uri("ms-appx:///" + filename);
    var image = new Image
    {
        Source = new BitmapImage(uri)
    };
    // fv is my flipview
    newSource.Add(image);
}
fv.ItemsSource = newSource;

当您设置 ListView 的 ItemsSource 时,ListView 将为每个对象生成一个项目。这些项目中的每一项都将对象设置为 DataContext。当您使用绑定(bind)表达式时,您告诉 XAML 从当前 DataContext 加载给定属性。 {Binding PathToIMage} 将从设置为 DataContext 的对象中获取 PathToImage 的值。 (作为旁注:不指定路径,只写 {Binding} 获取整个对象。因此,如果将字符串设置为 DataContext,{Binding} 将返回字符串的值。)

您可以阅读有关数据绑定(bind)的信息 here .

我认为只需将以下行粘贴到您的代码中即可工作:
改变这个...

String path = Directory.GetCurrentDirectory() + @"\Imports";
foreach (String imgurl in Directory.GetFiles(path))
{
    String filename = Path.GetFileName(imgurl);
    Uri uri = new Uri("ms-appx:///" + filename);
    var image = new Image
    {
        Source = new BitmapImage(uri)
    };
    // fv is my flipview
    fv.Items.Add(image);
}

...为此..

String path = Directory.GetCurrentDirectory() + @"\Imports";
fv.ItemsSource = Directory.GetFiles(path).Select(p => "ms-appx:///" + p);

...还有这个...

<Image Source="{Binding PathToImage}" Stretch="UniformToFill" />

...为此

<Image Source="{Binding}" Stretch="UniformToFill" />

关于c# - Windows 10 IoT Xaml动态添加图片到flipview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31871200/

相关文章:

wpf - 更改用户控件的颜色

c# - 如何将属性名称转换为 WPF 中的框架元素?

c# - 有没有办法在另一个进程正在读取时写入文件?

c# - C# 中是否有带参数约束的泛型构造函数?

c# - 如何在 Silverlight 中的 DataGrid 末尾包含自定义行?

wpf - UWP 中的绑定(bind)验证

.net - 通用应用的主题资源

c# - 2 对单个 C# 类的 XAML 引用(通用应用程序 8.1)

c# - 在 C# 中是一个 for(;;) 安全的,它到底做了什么?

c# - 如何检测断开连接的套接字 C#?