小编Cle*_*ens的帖子

使用 c# 设置 mediaElement 的来源

我正在尝试使用 c# 编码设置媒体元素的来源(视频位于项目的资产文件夹中),我的编码如下:

//Defined as such in my XAML
mediaElement.Source = new Uri(@"Assets\HarlemCampus.wmv");
mediaElement.Play();
Run Code Online (Sandbox Code Playgroud)

...但是当我单击按钮播放视频时收到以下错误:

在此处输入图片说明

我不想在 XAML 中预设源,因为我想根据用户在运行时选择的单选按钮动态更改媒体元素的源,如果有办法做到这一点?

c# xaml

2
推荐指数
1
解决办法
5021
查看次数

如何在wpf中清除画布的内容?

如何在wpf中清除画布的内容?尝试canvas.clear()不起作用.另外如何在wpf应用程序中添加缩放控件?使用滚动条缩放以移动图像.

任何帮助是极大的赞赏

c# wpf canvas zooming

2
推荐指数
1
解决办法
9939
查看次数

如何异步加载和显示图像

我是 WPF 新手,但我已经使用 C# 有一段时间了,目前正在开发一个简单的窗口(Windows 桌面),该窗口应该可视化目录中的所有照片。应用程序还应该了解 EXIF 数据,例如 ISO、光圈等,我为此使用了 DLL。

我定义了一个Photo类:

public class Photo {

    public string FileName { get; set; }
    public int ISO { get; set; }
    ...
}
Run Code Online (Sandbox Code Playgroud)

我想List<Photo>在运行时存储在 a 中。

然后我声明了一个PhotoItem(XAML 用户控件),其中包含一个图像控件和一个 TextBlock。对于每一个Photo创建的对象,都会有一个PhotoItem创建对象将相应的Photo属性保存为:

public partial class PhotoItem : UserControl {
    ...
    public Photo Photo { get; set; }
    ...
}
Run Code Online (Sandbox Code Playgroud)

从这个Photo属性PhotoItem中,知道在哪里寻找图像以及要显示什么 ISO 等。

现在我的问题。因为如果用户选择目录,加载图像本身以及元数据会花费太长时间,所以我想首先将所有 s 添加PhotoItem到窗口(仍然为空),然后运行元数据查找和图像缩略图加载对于他们每个人来说。当然,如果这些操作不阻塞 UI 线程那就最好了,因此我目前使用一个 …

c# wpf xaml multithreading

1
推荐指数
1
解决办法
2686
查看次数

如何从几种不同图像格式的字节数组中获取BitmapImage

我已使用以下方法将图像保存在数据库中,将它们转换为不同图像格式的字节数组:

public byte[] foo()
{
    Image img = Image.FromFile(path);
    var tmpStream = new MemoryStream();
    ImageFormat format = img.RawFormat;
    img.Save(tmpStream, format);
    tmpStream.Seek(0, SeekOrigin.Begin);
    var imgBytes = new byte[MAX_IMG_SIZE];
    tmpStream.Read(imgBytes, 0, MAX_IMG_SIZE);
    return imgBytes;
}
Run Code Online (Sandbox Code Playgroud)

现在我需要读出它们并将它们转换回 BitmapImage 类型,以便我可以将它们显示给用户。我正在考虑使用 Image.FromStream(Stream) 方法,但这似乎没有考虑到不同的 ImageFormats...有人知道该怎么做吗?提前致谢。

c# wpf image

1
推荐指数
1
解决办法
2732
查看次数

XAML 密钥绑定 1,2,3

我决定通过按页面上的键来复制按钮,但收到如下错误:

“1”不能用作“Key”的值。数字不是有效的枚举值。

 <Page.InputBindings>
    <KeyBinding Command="{Binding Btn_Click}" 
               CommandParameter="{Binding ElementName=btn1,Path=Content}"
                Key="1"/>
    <KeyBinding Command="{Binding Btn_Click}" 
               CommandParameter="{Binding ElementName=btn2,Path=Content}"
                Key="2"/>
    <KeyBinding Command="{Binding Btn_Click}" 
               CommandParameter="{Binding ElementName=btn3,Path=Content}"
                Key="3"/>
    <KeyBinding Command="{Binding Btn_Click}" 
               CommandParameter="{Binding ElementName=btn4,Path=Content}"
                Key="4"/>
</Page.InputBindings>
Run Code Online (Sandbox Code Playgroud)

我可以欺骗系统吗?

c# keyboard wpf xaml binding

1
推荐指数
1
解决办法
946
查看次数

如何只读取.txt文件的第二行?

要读取wpf应用程序中.txt文件的第一行,我们可以使用以下代码行:

string line1 = File.ReadLines("MyFile.txt").First(); // gets the first line
Run Code Online (Sandbox Code Playgroud)

但现在,我怎么能只阅读我文件的第二行?

c#

1
推荐指数
1
解决办法
510
查看次数

为什么程序中无法正确显示 SVG 图像?

我想在 WPF 中显示 SVG 文件,我发现很多人推荐使用sharpvectors.

我将它与 XAML 一起使用,如下所示:

<Window x:Class="KongGamLung.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:KongGamLung"        
        xmlns:SVG="http://sharpvectors.codeplex.com/svgc/"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <SVG:SvgViewbox Source="Create.svg" AutoSize="True" OptimizePath="False" TextAsGeometry="True"></SVG:SvgViewbox>
    </Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)

那么,SVG 在编辑器中正确显示: 在此处输入图片说明

但是,在我运行程序后,SVG 不见了。 在此处输入图片说明

为什么会变成这样?更重要的是,我在 NUGET 中尝试了几个 SVG 包。但它们比这更糟糕,即使在编辑器中也无法显示。

我该如何解决这个问题?如果有比这更好的 SVG 包,请推荐给我。谢谢你。


在此处输入图片说明

wpf

1
推荐指数
1
解决办法
878
查看次数

为什么这段代码会抛出'System.ArgumentOutOfRangeException'(SQlite)?

我无法理解为什么会引发此异常!

这是代码:

public static List<Employee> LoadEmployees()
        {
            List<Employee> emp = new List<Employee>();
            try
            {
                using (SQLiteConnection cnn = new SQLiteConnection(LoadConnectionString()))
                {
                    cnn.Open();                   
                    string query = "select * from Employee;";
                    SQLiteCommand cmd = new SQLiteCommand(query, cnn);
                    using (var reader = cmd.ExecuteReader())
                    {
                        int i = 0;
                        while (reader.Read())
                        {
                            emp[i].Id = reader.GetString(0);
                            emp[i].Name = reader.GetString(1);
                            emp[i].Gender = reader.GetString(2);
                            emp[i].Department = reader.GetString(3);
                            emp[i].Doj = reader.GetString(4);
                            emp[i].Email = reader.GetString(5);
                            emp[i].Phone = reader.GetString(6);
                            i++;
                        }

                    }
                    return emp;
                }

            }
            catch (Exception ex)
            { …
Run Code Online (Sandbox Code Playgroud)

c# sqlite exception

1
推荐指数
1
解决办法
53
查看次数

为什么此语法在 C# 中有效?

在我得到的 WPF 项目中,我有一个名为 IHavePassword 的类,在另一个文件中是一个像这样的按钮的侦听器

private void DoLogin(object parameter)
{
    if (parameter is IHavePassword passwordContainer)
    {
        ...
    }
}
Run Code Online (Sandbox Code Playgroud)

我的问题与“is”关键字之后的 if 语句有关,它在那里做什么?是否将方法参数与“IHavePassword”类新实例进行比较?

c#

1
推荐指数
1
解决办法
101
查看次数

在Where子句中使用Substring的Directory.GetFiles上的LINQ

我有一个目录,其中有两个以"L"开头的*.xml文件.我尝试了各种LINQ语法,但都没有返回任何文件.它应该很简单,但我无法解决这个问题.

IEnumerable<string> files = Directory.EnumerateFiles(path)
    .Where(p => p.Substring(0,1) == "L");

IEnumerable<string> files = Directory.GetFiles(path, "*.xml")
    .Where(p => (p.Substring(0,1) == "L"))

IEnumerable<string> files = Directory.EnumerateFiles(path)
    .Where(p => p.StartsWith("L"))
Run Code Online (Sandbox Code Playgroud)

c# linq substring

0
推荐指数
2
解决办法
426
查看次数

通知静态类中静态属性的绑定

我在静态类中找到了静态属性的通知属性更改示例.但它不会更新TextBlock中的任何更改.这是代码.

第一个绑定在构造函数中使用"test"字符串,但StaticPropertyChanged始终为null.

public static class InteractionData
{
    public static List<string> SelectedDirectories { get; set; }
    private static string errorMessage { get; set; }
    public static string ErrorMessgae
    {
        get { return errorMessage; }
        set
        {
            errorMessage = value;
            NotifyStaticPropertyChanged("errorMessage");
        }
    }

    static InteractionData()
    {
        SelectedDirectories = new List<string>();
        errorMessage = "test";
    }

    public static event EventHandler<PropertyChangedEventArgs> StaticPropertyChanged;
    private static void NotifyStaticPropertyChanged(string propertyName)
    {
        if (StaticPropertyChanged != null)
            StaticPropertyChanged(null, new PropertyChangedEventArgs(propertyName));
    }
}
Run Code Online (Sandbox Code Playgroud)

在视图中......

     xmlns:error ="clr-namespace:CopyBackup.Providers"
<TextBlock …
Run Code Online (Sandbox Code Playgroud)

c# data-binding wpf

-2
推荐指数
1
解决办法
1393
查看次数