相关疑难解决方法(0)

WPF编程方法论

在WPF编程我的APP 3个月后,我第二次想到了我编写应用程序的方式(我知道这可能为时已晚).在我的APP上,我使用的是我的工具管理的软件的API.我的DAL包含16个类,其中3个是单例.我在.cs文件中有一些逻辑,而且XAML偏离正轨.我的问题是,我看到很多评论认为用WPF编写的应用程序应该使用MVVM,这将使代码更具可用性和可读性,我可以将我的代码转换为MVVM吗?什么是MVVM的实际含义(不是维基百科或手册定义)?

我也使用SQL查询,我读了一篇关于EF(实体框架)的论文,MVVM和EF可以在同一个项目中共存吗?

我知道我的问题是一个新手问题(我是新手:P)和一个抽象的问题,但我想知道我写的应用程序将是我此时写的最好的:)

wpf entity-framework mvvm .net-4.5

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

MVVM for winforms

可能重复:
Windows窗体的UI设计模式(如WPF的MVVM)

MVVM应该用于WinForms吗?如果是这样,使用MVP的优势是什么?

mvvm winforms

32
推荐指数
3
解决办法
4万
查看次数

XAML UserControl继承

来自Java,我真的习惯于制作GUI组件的常用做法:我通常会做一些基类,它包含GUI组件的所有常用对象,然后我扩展它.

所以,基本上,这是我想用C#和XAML实现的.

为了使问题清楚,这是我正在做的一个例子(那是行不通的!):

我们有一个拥有自己的XAML的基类

<UserControl x:Class="BaseClass"
    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"
    mc:Ignorable="d"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    d:DesignHeight="480" d:DesignWidth="480">

    <Grid x:Name="LayoutRoot" Background="{StaticResource PhoneChromeBrush}">
        <Border BorderBrush="Aqua" BorderThickness="10" CornerRadius="10" x:Name="Border" HorizontalAlignment="Left" Height="480" VerticalAlignment="Top" Width="480"/>

    </Grid>
</UserControl>
Run Code Online (Sandbox Code Playgroud)

然后我们有一个扩展第一个类的类

<base:BaseClass x:Class="DerivedClass"
    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:base="clr-namespace:BaseClass"
    mc:Ignorable="d"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    d:DesignHeight="60" d:DesignWidth="200">

    <Grid x:Name="LayoutRoot" Margin="0" Width="200" Height="60" MaxWidth="200" MaxHeight="60" Background="{StaticResource PhoneAccentBrush}">        
        <TextBlock x:Name="dummyText" HorizontalAlignment="Left" Margin="10,10,0,0" TextWrapping="Wrap" Text="Dummy Plugin" VerticalAlignment="Top" Height="40" Width="180" Foreground="White" TextAlignment="Center"/>
    </Grid>
</base:BaseClass>
Run Code Online (Sandbox Code Playgroud)

从2个XAML代码开始,我想要做的就是DerivedClass进入BaseClass容器.这将允许我在各种派生类之间共享组件,而不必在每次需要时编写代码.

例如,如果我希望我的所有组件都有这个圆形边框,我想把它放在bass类中,然后将它放在所有派生类中,而不必重写它. …

c# wpf inheritance xaml windows-phone-8

24
推荐指数
1
解决办法
3万
查看次数

WPF和Winforms之间是否存在性能差异?

标题基本概括了所有内容.我做了一些谷歌搜索,但我能找到的唯一信息是至少一年,有些信息比Windows 7旧.所以我很好奇,使用WPF会有性能损失吗?

对于某些事情(例如数据绑定)来说,它比你在WinForms上一起攻击的速度更快吗?

这将是一个用.NET创建的项目,以防对任何人都很重要.

编辑

我正在看的这个项目有一个基本的UI风格,但是可能会显示很多信息(它查询文件/目录并在屏幕上显示它们).我还没有决定它是否会自动刷新屏幕,但我想它可能会.

可能会有相当多的控制,但任何人都不应该考虑"图形"密集.

从业务应用程序的角度来看,我正在关注性能.

我不打算使用第三方控件.

wpf winforms

20
推荐指数
4
解决办法
2万
查看次数

将Winform应用程序转换为WPF应用程序

我有一个用WinForms制作的小程序,现在我想用WPF重新制作它.

我是WPF的新手,我读到无论你用XAML做什么,你也可以没有它,这意味着只使用代码.

当然,使用Winforms时没有任何XAML.我可以对用于winforms应用程序的WPF应用程序使用相同的代码并获得相同的结果吗?或者我是否需要创建和编辑XAML?使用或不使用XAML有什么好处?

另外,考虑到使用Winforms的过去经验,我是否应该以某种方式改变我对Winforms的设计和实现的思考方式,但是不适合WPF?

.net c# wpf xaml winforms

16
推荐指数
1
解决办法
5万
查看次数

在'对象'上找不到WPF/XAML属性

我在新的WPF应用程序中使用BackgroundWorker,我需要报告进度/更新UI,因为它在后台运行.

我在WIndows Forms应用程序中已经做了很长时间了.我刚刚为WPF重写了这一切,这让我有点头疼.

它在运行时不断抛出以下错误:

System.Windows.Data错误:40:BindingExpression路径错误:'object'''Char'(HashCode = 5046349)'上找不到'Sender'属性.BindingExpression:路径=发件人; DataItem ='Char'(HashCode = 5046349); target元素是'TextBlock'(Name =''); 目标属性是"文本"(类型"字符串")System.Windows.Data错误:40:BindingExpression路径错误:对"对象"""字符"(的HashCode = 5046349)"未找到"主题"属性.BindingExpression:路径=主题; DataItem ='Char'(HashCode = 5046349); target元素是'TextBlock'(Name =''); 目标属性是'文字'(类型'字符串')System.Windows.Data错误:40:BindingExpression路径错误:'对象'上找不到'发件人'属性''字符'(HashCode = 6619237)'.BindingExpression:路径=发件人; DataItem ='Char'(HashCode = 6619237); target元素是'TextBlock'(Name =''); 目标属性是"文本"(类型"字符串")System.Windows.Data错误:40:BindingExpression路径错误:对"对象"""字符"(的HashCode = 6619237)"未找到"主题"属性.BindingExpression:路径=主题; DataItem ='Char'(HashCode = 6619237); target元素是'TextBlock'(Name =''); target属性为'Text'(类型'String')System.Windows.Data错误:40:BindingExpression路径错误:'object'''Char'(HashCode = 7536755)'找不到'Sender'属性.BindingExpression:路径=发件人; DataItem ='Char'(HashCode = 7536755); target元素是'TextBlock'(Name =''); 目标属性是"文本"(类型"字符串")System.Windows.Data错误:40:BindingExpression路径错误:对"对象"""字符"(的HashCode = 7536755)"未找到"主题"属性.BindingExpression:路径=主题; DataItem ='Char'(HashCode = 7536755); target元素是'TextBlock'(Name =''); target属性为'Text'(类型'String')System.Windows.Data错误:40:BindingExpression路径错误:'object'''Char'(HashCode = 7536755)'找不到'Sender'属性.BindingExpression:路径=发件人; DataItem ='Char'(HashCode = 7536755); target元素是'TextBlock'(Name =''); 目标属性是"文本"(类型"字符串")System.Windows.Data错误:40:BindingExpression路径错误:对"对象"""字符"(的HashCode = 7536755)"未找到"主题"属性.BindingExpression:路径=主题; DataItem ='Char'(HashCode = 7536755); target元素是'TextBlock'(Name =''); target属性为'Text'(类型'String')System.Windows.Data错误:40:BindingExpression路径错误:'object'''Char'(HashCode = 6357089)'找不到'Sender'属性.BindingExpression:路径=发件人; DataItem …

.net c# wpf xaml

11
推荐指数
1
解决办法
3万
查看次数

如何在Wpf中以编程方式在网格的行内创建网格

我在WPF的工作-有一个buttonclick event handler我的应用程序.当我点击按钮时,它的事件处理程序在网格中生成一个名为as的新行grids.在这个新的行中,我想以编程方式添加另一个网格,以便在此行的网格中添加Label,Button和TextBox.当我执行我的代码时,它只生成一个texboxes!标签和按钮显示一次!这里的代码和图片是:请随时询问我的查询是否不清楚!

 int r =0;
 private void button2_Click(object sender, RoutedEventArgs e)
    {
        TextEdit text1; Button button1; Grid grid1;
        grids.RowDefinitions.Add(new RowDefinition());
        text1 = new TextEdit();
        text1.SetValue(Grid.ColumnProperty, 1);
        text1.SetValue(Grid.RowProperty, r);
        button1 = new Button();
        button1.Content = "Left + " + r;
        button1.Click += new RoutedEventHandler(button1_Click);
        button1.SetValue(Grid.ColumnProperty, 1);
        button1.SetValue(Grid.RowProperty, r);
        grid1 = new Grid();
        grid1.SetValue(Grid.ColumnProperty, 1);
        grids.RowDefinitions.Add(new RowDefinition());
        grid1.SetValue(Grid.RowProperty, r);
        grids.Children.Add(button1);
        grids.Children.Add(text1);
        r = r + 1;
    }
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述 编辑

 int r =0;
 private void button2_Click(object …
Run Code Online (Sandbox Code Playgroud)

.net c# wpf

9
推荐指数
2
解决办法
2万
查看次数

WPF TabControl,用C#代码更改TabItem的背景颜色

嗨,我认为这是初学者的问题.我搜索了所有相关问题.但是所有这些都是由.xaml回答的.但是,我需要的是后面的代码.我有一个TabControl.我需要设置其项目的背景颜色.选择,取消选择和悬停时,我需要为项目设置不同的颜色.非常感谢你的帮助.我想在这里发布我的代码.但是,目前,我所拥有的只是TabControl的一个实例和一个名为ActiveTabIndex的属性.

---------------------------------------编辑1 --------- --------------------------------------

我添加了一个事件SelectionChanged

(this.myTabControl as System.Windows.Controls.TabControl).SelectionChanged + = TabSet_SelectionChanged;

void TabSet_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            foreach (System.Windows.Controls.TabItem item in (this.myTabControl as System.Windows.Controls.TabControl).Items)
            {
                if (item == (this.myTabControl as System.Windows.Controls.TabControl).SelectedItem)
                {
                    item.Background = System.Windows.Media.Brushes.Red;
                }
                else
                    item.Background = System.Windows.Media.Brushes.Green;
            }
        }
Run Code Online (Sandbox Code Playgroud)

但是,我实际上只能设置绿色.所选项目的背景颜色保留为默认颜色而不是红色.关于这个的任何提示?另外,我想知道如何为悬停添加事件.没有找到确切的事件.再次感谢.

-----------------------编辑2 ------------------------- -----

经过长时间的长时间讨论.我决定(实际上不是我的决定)使用XAML.是啊.我是WPF的新手.所以我对此仍有疑问(对此我很抱歉,请耐心等待).问题在这里:当鼠标悬停在TabItem上时,我想将背景颜色更改为橙​​色.现在,当鼠标位于ContentPanel和TabItem上时,颜色为橙色.当鼠标只在TabItem上时我需要它是橙色的.(我不确定我是否足够清楚.)另一个问题是我会让用户设置颜色而不是直接设置为红色.我想我需要一些绑定.对于这个问题,我稍后会谷歌肯定.只想清楚.非常感谢你们所有人.真有帮助.

<TabItem x:Class="MyControls.Tab"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <TabItem.Style>
        <Style TargetType="{x:Type TabItem}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TabItem}">
                        <Grid>
                            <Border  Name="Border" Margin="0,0,-4,0" BorderThickness="1,1,1,1" CornerRadius="2,12,0,0" >
                                <ContentPresenter x:Name="ContentSite" VerticalAlignment="Center" HorizontalAlignment="Center" ContentSource="Header" Margin="12,2,12,2" RecognizesAccessKey="True"/> …
Run Code Online (Sandbox Code Playgroud)

c# wpf

6
推荐指数
2
解决办法
2万
查看次数

如何在不同的线程上创建用户控件?

我正在创建一个UI,它将显示类似于分形和蜂窝自动化的生成模式,但它们将不断生成和自动化.

在此输入图像描述

像素和像素颜色将在用户控件中显示为正方形网格.我已经创建了usercontrol来显示它,但是因为它在每个计时器上不断计算.它会大大减慢UI的其余部分并导致所有其他元素断断续续.

所以我研究了线程并在BackgroundWorker DoWork()中设置了"计算"部分,最终并没有以我想要的方式运行.BackgroundWorker正在使用来自主线程(Rectangle [400])的数据,所以我不得不使用Dispatcher.Invoke(new Action(() => { }));它只是打败了目的,程序仍然非常"stuttery".

那么,我怎么能在一个单独的线程上创建一个usercontrol ... name:display_control并让它出现在另一个usercontrol中...... name:unsercontrol1(在主线程中运行),?然后我可以使用usercontrol1 User_Input_Class实例对user_input数据进行数据绑定.

或者,有没有更好的方法来实现这一目标?只有我能想到这样做的其他方法是简单地为显示创建一个完全独立的程序,并共享一个包含user_input数据的文件,这是非常不专业的.

public partial class Fractal_Gen_A : UserControl
{
    byte W_R = 0;
    byte W_G = 255;
    byte W_B = 0;

    int Pixel_Max_Width = 20;
    int Pixel_Max_Height = 20;

    Color[] Pixel_Color = new Color[20 * 20]; //Width_Max * Canvas_Height_Count
    Rectangle[] Pixel = new Rectangle[20 * 20];
    Color[] Temp_Color = new Color[20 * 20];

    BackgroundWorker worker = new BackgroundWorker();
    private void Timer_Tick(object sender, EventArgs e) …
Run Code Online (Sandbox Code Playgroud)

c# wpf user-controls multithreading image

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

WPF UI多任务处理

我正在以编程方式创建一些UI,这涉及一些繁重的处理.基本上我想要的是在我的UI被构建/添加到窗口时运行加载器动画.添加的UI是一些网格,一些图像被加载到它们中.

到目前为止,我已经尝试过BackgroundWorker,但由于我需要使用UI线程来添加我正在构建的UI,因此在添加的UI完成之前,加载器将无法启动/动画.

我也尝试过一些没有任何结果的异步方法.我的最后一次尝试是这样的,但它仍然需要访问UI线程,这最终将冻结UI动画,直到工作完成

   private async void begin()
    {

        await this.LongRunOpAsync();

    }

    public Task LongRunOpAsync()
    {
        return Task.Run(() =>
        {
            InitGrid();
            stopLoadingScreen();
        });
    }
Run Code Online (Sandbox Code Playgroud)

我可能会遗漏一些东西,但我不知道什么,我也没有想法这样做.任何帮助,将不胜感激.

编辑:有繁重工作的方法

private void makeIgrid()
        {
            Grid hostgrid = new Grid();
            hostgrid.Name = "imagesHostGrid";
            hostgrid.Width = 700;
            hostgrid.VerticalAlignment = VerticalAlignment.Top;
            hostgrid.HorizontalAlignment = HorizontalAlignment.Center;
            hostgrid.SetValue(Canvas.ZIndexProperty, 0);
            this.RegisterName(hostgrid.Name, hostgrid);

            Grid imagegrid = new Grid();
            imagegrid.Name = "imagegrid";
            imagegrid.Height = height2;
            //imagegrid.Width = 700;
            imagegrid.SetValue(Canvas.ZIndexProperty, 0);
            imagegrid.VerticalAlignment = VerticalAlignment.Top;
            imagegrid.HorizontalAlignment = HorizontalAlignment.Center;
            imagegrid.Margin = new Thickness(0, height1, 0, 0);//(left,top,right,bottom)

            RowDefinition …
Run Code Online (Sandbox Code Playgroud)

c# wpf multithreading

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