相关疑难解决方法(0)

你如何从xaml传递参数?

我创建了自己的UserControl"ClockControl",我通过主窗口的XAML初始化.

唯一的问题是我必须将一个参数传递给时钟控件的构造函数,我不知道我该如何做到这一点.

如果我没有参数,这可以工作:

<myControl:ClockControl></myControl:ClockControl>
Run Code Online (Sandbox Code Playgroud)

但是我如何传递参数呢?

这是构造函数:

public ClockControl(String city)
    {
        InitializeComponent();
        this.initController();
        ......
        .....
    }
Run Code Online (Sandbox Code Playgroud)

提前致谢.

c# wpf xaml

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

如何修复:这里不允许使用XAML2009语言构造?

在我的App.xaml代码中,我有以下代码:

<Application x:Class="PcgTools.App"
    ...
    <Application.Resources>
        <ResourceDictionary>
             ...
            <res:Strings x:FactoryMethod="res:StringResourceHelper.GetStringResources" 
             x:Key="LocStrings"/>
        </ResourceDictionary> 
    </Application.Resources>
</Application>
Run Code Online (Sandbox Code Playgroud)

在使用FactoryMethod或Attribute构造之前,我从VS2012编译器获得了一个错误.但现在我得到错误:这里不允许使用Xaml2009语言构造.

如何解决这个问题?

wpf xaml visual-studio-2012

7
推荐指数
2
解决办法
5537
查看次数

在XAML中命名没有默认构造函数的用户控件

我有一个没有无参数构造函数的用户控件; 我们称之为WithoutDefaultConstructor.我想将一个WithoutDefaultConstructor被调用的myControl插入另一个控件(被调用MainWindow)的XAML代码中.但是,我收到此编译器错误:

"WithoutDefaultConstructor"类型不能具有Name属性.没有默认构造函数的值类型和类型可以用作ResourceDictionary中的项.

如何在不添加无参数构造函数的情况下解决此问题WithoutDefaultConstructor

以下是内容MainWindow.xaml:

<Window x:Class="WpfApplication1.MainWindow"
 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 xmlns:WpfApplication1="clr-namespace:WpfApplication1" Title="MainWindow" 
 Height="350" Width="525">
    <WpfApplication1:WithoutDefaultConstructor Name="myControl">
    </WpfApplication1:WithoutDefaultConstructor>
</Window>
Run Code Online (Sandbox Code Playgroud)

以下是内容WithoutDefaultConstructor.xaml.cs:

using System.Windows.Controls;

namespace WpfApplication1
{
    public partial class WithoutDefaultConstructor : UserControl
    {
        private int I_really_need_to_initialize_this_int;

        public WithoutDefaultConstructor(int i)
        {
            InitializeComponent();
            I_really_need_to_initialize_this_int = i;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

wpf xaml default-constructor

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

从XAML将ViewModel传递给User Control构造函数

在我的MVVM应用程序中,我正在尝试创建一个可在多个应用程序之间共享的可重用用户控件,这里是构造函数

public MyUserControl(IMyViewModel viewModel)
{
    InitializeComponent();
    DataContext = viewModel;
}
Run Code Online (Sandbox Code Playgroud)

所以我的计划是每个应用程序只将它自己的viewmodel提供给共享视图.

现在的问题是如何将viewmodel对象从XAML代码传递给构造函数:

<my:MyUserControl "somehow pass a viewmodel object from here">
Run Code Online (Sandbox Code Playgroud)

谢谢

.net c# wpf dependency-injection mvvm

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