小编Edw*_*uay的帖子

使用Predicate/CreateDelegate有什么问题?

我正在创建一个带代理的简单代码生成器.

为什么我在运行时收到此错误:

绑定目标方法时出错.

在以下代码?

XAML:

<Window x:Class="Parser.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Loaded="Window_Loaded"
    Title="Parser" Height="600" Width="800">

        <TextBox x:Name="Output"
                 VerticalScrollBarVisibility="Visible"
                 Margin="10"/>
</Window>
Run Code Online (Sandbox Code Playgroud)

代码隐藏:

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;

namespace Parser
{
    public partial class Window1 : Window
    {
        private List<string> _fields;

        public Window1()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            _fields = new List<string> { "CustomerID", 
                "CompanyName",
                "ContactName",
                "ContactTitle",
                "Address",
                "City",
                "Region",
                "PostalCode",
                "Country",
                "Phone",
                "Fax"
            };

            Output.Text += ParseFieldsWithMethod("BuildAssignmentLines");
            Output.Text += ParseFieldsWithMethod("BuildEnabledLines");
        }

        private string ParseFieldsWithMethod(string theParseMethod) …
Run Code Online (Sandbox Code Playgroud)

c# delegates predicate

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

在XAML TabControl中,如何设置焦点选项卡标题的样式?

我可以使用TabItem.Background设置每个TabItem的背景,但是当选择该选项卡时,它是普通的香草白色.

如何设置聚焦的选项卡标题的样式?

<TabControl DockPanel.Dock="Top">
    <TabControl.Background>
        <LinearGradientBrush EndPoint="1.115,1.13" StartPoint="0,-0.02">
            <GradientStop Color="#FFFFFFFF" Offset="1"/>
            <GradientStop Color="#FFE0E376" Offset="0"/>
        </LinearGradientBrush>
    </TabControl.Background>

    <TabItem Header="Allgem." Cursor="Hand">
        <TabItem.Background>
            <LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
                <GradientStop Color="#FFF3F3F3" Offset="0"/>
                <GradientStop Color="#FFF11818" Offset="1"/>
            </LinearGradientBrush>
        </TabItem.Background>
        <StackPanel DockPanel.Dock="Bottom" Width="400" HorizontalAlignment="Left" Margin="10">
            ...
Run Code Online (Sandbox Code Playgroud)

xaml tabcontrol

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

没有DependencyProperties与实际属性冲突的最佳方法是什么?

我发现当我创建依赖属性时,大多数都与UserControl中的属性名称冲突,例如背景,宽度等.所以我的策略是在我的所有自定义属性前加上"The",所以我有,例如

  • 的背景
  • 宽度

等等

我尝试使用"new"关键字来消除警告,但这会导致运行时发生冲突.

有没有人在自定义用户控件中为DependencyProperties提供更好的命名策略?

public partial class SmartForm : UserControl
{
    public SmartForm()
    {
        InitializeComponent();
        DataContext = this;

        TheBackground = "#FFD700";
    }

    #region DependencyProperty: TheBackground
    public string TheBackground
    {
        get
        {
            return (string)GetValue(TheBackgroundProperty);
        }
        set
        {
            SetValue(TheBackgroundProperty, value);
        }
    }

    public static readonly DependencyProperty TheBackgroundProperty =
        DependencyProperty.Register("TheBackground", typeof(string), typeof(SmartForm),
        new FrameworkPropertyMetadata());
    #endregion
}
Run Code Online (Sandbox Code Playgroud)

wpf user-controls dependency-properties

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

如何将两个附加行为附加到一个XAML元素?

我已经实现了这里找到附加命令行为模式,它可以很好地允许例如一个Border有一个在ViewModel中触发的左键或右键单击事件:

XAML:

<Border Background="Yellow" Width="350" Margin="0,0,10,0" Height="35" CornerRadius="2"
        c:CommandBehavior.Event="MouseLeftButtonDown" 
        c:CommandBehavior.Command="{Binding PressedLeftButton}"
        c:CommandBehavior.CommandParameter="MainBorder123">
    <TextBlock Text="this is the click area"/>
</Border>
Run Code Online (Sandbox Code Playgroud)

代码背后:

public ICommand PressedLeftButton { get; private set; }

public MainViewModel()
{

    Output = "original value";

    PressedLeftButton = new SimpleCommand
    {
        ExecuteDelegate = parameterValue => {
            Output = String.Format("left mouse button was pressed at {0} and sent the parameter value \"{1}\"", DateTime.Now.ToString(), parameterValue.ToString());
        }
    };
}
Run Code Online (Sandbox Code Playgroud)

但是,如何将两个附加行为附加到一个元素,例如,我想要执行以下操作,但它当然会给我一个错误:

<Border Background="Yellow" Width="350" Margin="0,0,10,0" Height="35" CornerRadius="2" …
Run Code Online (Sandbox Code Playgroud)

wpf mvvm attachedbehaviors

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

如何强制C#构造函数成为工厂?

下面的代码包含两个类:

  • SmartForm(简单模型类)
  • SmartForms(包含一组SmartForm对象的复数类)

我希望能够像这样实例化单数和复数类(即我不想要工厂方法GetSmartForm()):

SmartForms smartForms = new SmartForms("all");
SmartForm smartForm = new SmartForm("id = 34");
Run Code Online (Sandbox Code Playgroud)

要合并逻辑,只有复数类才能访问数据库.当被要求实例化时,单数类将简单地实例化一个复数类,然后从复数对象的集合中选择一个对象并成为该对象.

我怎么做?我试图分配this不起作用的对象.

using System.Collections.Generic;

namespace TestFactory234
{
    public class Program
    {
        static void Main(string[] args)
        {
            SmartForms smartForms = new SmartForms("all");
            SmartForm smartForm = new SmartForm("id = 34");
        }
    }

    public class SmartForm
    {
        private string _loadCode;

        public string IdCode { get; set; }
        public string Title { get; …
Run Code Online (Sandbox Code Playgroud)

c# constructor instantiation

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

使用C#泛型作为构造函数名称的语法是什么?

我有很多继承自的课程Item<T>.

每个类都有一个我想要进入的Create()方法Item<T>.

然而,以下代码获取错误" 无法创建变量类型'T'的实例,因为它没有new()约束 ":

T item = new T(loadCode);
Run Code Online (Sandbox Code Playgroud)

执行此操作的更正语法是什么?

public abstract class Item<T> : ItemBase
{

    public static T Create(string loadCode)
    {
        T item = new T(loadCode);

        if (!item.IsEmpty())
        {
            return item;
        }
        else
        {
            throw new CannotInstantiateException();
        }
    }
Run Code Online (Sandbox Code Playgroud)

c# generics inheritance

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

ViewModel将其自身注入其子ViewModel是否正确?

我创建了一个MVVM测试应用程序,它在运行时读取XML文件以动态创建菜单,并根据用户选择的内容,动态加载该页面的UserControl.结果是一个很好的MVVM模式,允许您在XML文件中定义每页一个View/ViewModel对.非常好.

所以现在我只是添加了开发人员在一个页面上创建一个按钮的功能,该按钮可以转到另一个页面.我这样做的方法是在MainViewModel中构建ObservableCollection中的ViewModel集合,当我构建每个时,我将MainViewModel本身(this)注入每个UserControl-ViewModel的构造函数中.这样每个UserControl都包含MainViewModel,以便开发人员可以通过MainViewModel操作应用程序(例如调用SwitchPage(idCode)).

此外,我需要保存的任何全局状态都可以保存在每个UserControl都可以访问的MainViewModel中.

此外,每个UserControl(PageItem)都可以完全访问其他每个UserControl,这使我可以从任何UserControl基本上控制应用程序中的任何内容,这是我一直试图在MVVM应用程序中实现的很长一段时间.

所以我的问题是:这种ViewModel注入是一种有用/已知的模式,还是将ViewModel注入其子ViewModel时会出现问题?它似乎递归我,但似乎工作正常,并给了我迄今为止我想要的功能.根据我从Composite Application Library体系结构中学到的东西,这似乎与那里发生的事情类似,例如在下面的代码中,我可以根据需要将其他应用程序对象注入到我的ViewModel中.

public MainViewModel()
{

    PageItems pageItems = PageItems.Create("all");

    foreach (PageItem pageItem in pageItems.Collection)
    {
        string assemblyName = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;
        string viewModelName = assemblyName + ".ViewModels.PageItem" + StringHelpers.ForcePascalNotation(pageItem.IdCode) + "ViewModel";
        var type = Type.GetType(viewModelName);
        var viewModel = Activator.CreateInstance(type, this, pageItem) as ViewModelPageItemBase;
        AllPageItemViewModels.Add(viewModel);
    }

    CurrentPageItemViewModelIndex = 0;
    LoadCurrentPageItemViewModel();
}
Run Code Online (Sandbox Code Playgroud)

wpf mvvm application-design

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

为什么我注入的依赖项对象在每个模块中都不是同一个实例?

我正在使用Prism和Unity.

我有这个引导程序:

protected override IModuleCatalog GetModuleCatalog()
{
    ModuleCatalog catalog = new ModuleCatalog()
        .AddModule(typeof(CustomerModule.CustomerModule))
        .AddModule(typeof(EmployeesModule.EmployeesModule))
        .AddModule(typeof(MenuModule.MenuModule));
    return catalog;
}
Run Code Online (Sandbox Code Playgroud)

我的CustomerModule会注入一个MenuManager并向其添加菜单项:

public void Initialize()
{
    menuManager.MenuItems.Add("Customers");
    menuManager.MenuItems.Add("Other Customers");
}
Run Code Online (Sandbox Code Playgroud)

但是当我的MainMenuPresenter对象也被注入MenuManager时,它不是同一个对象:

public MainMenuPresenter(MainMenuView view, MenuManager menuManager)
{
    View = view;
    View.DataContext = this;

    foreach (string menuItemTitle in menuManager.MenuItems)
    {
        MenuItems.Add(menuItemTitle);
    }
}
Run Code Online (Sandbox Code Playgroud)

如何告诉Prism/Unity我希望注入的MenuManager是Singleton,以便将相同的对象注入到我的每个模块和对象中?

wpf dependency-injection prism

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

如何使用Split()重构此C#代码?

我怎样才能重构这一点,以便numberOfItems不必被声明为变量?

//method: gets the text in a string in front of a marker, if marker is not there, then return empty string
//example: GetTextAfterMarker("documents/jan/letter043.doc","/") returns "letter043.doc"
//example: GetTextAfterMarker("letter043.doc","/") returns ""
//example: GetTextAfterMarker("letter043.doc",".") returns "doc"
public static string GetTextAfterMarker(string line, string marker)
{
    int numberOfItems = line.Split(new string[] { marker }, StringSplitOptions.None).Count();

    string result = line.Split(new string[] { marker }, StringSplitOptions.None)[numberOfItems-1];
    return line.Equals(result) ? string.Empty : result;
}
Run Code Online (Sandbox Code Playgroud)

c# string refactoring

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

如何在jQuery中引用id + class的HTML元素?

在下面的javascript/html中,我可以引用div.answer但是如何引用div#flashcard-001.answer

HTML:

<div id="flashcard-001" class="flashcard">
    <div class="question">What color is the sky?</div>
    <div class="answer">blue</div>
    <button class="show">Show</button>
    <button class="hide">Hide</button>
</div>
Run Code Online (Sandbox Code Playgroud)

使用Javascript:

//run when page is loaded
google.setOnLoadCallback(function() {
    $("div.answer").hide(); //WORKS
    $("div#flashcard-001.answer").hide(); //DOES NOT WORK
    $("button.show").bind("click", function(e) {
        $("div.answer").show();
    });
    $("button.hide").bind("click", function(e) {
        $("div.answer").hide();
    });
});
Run Code Online (Sandbox Code Playgroud)

jquery

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