【问题标题】:How to connect xaml to it's xaml.cs using code如何使用代码将 xaml 连接到它的 xaml.cs
【发布时间】:2018-12-31 20:20:53
【问题描述】:

我正在创建一个文件生成器,它还会将添加的文件添加到更改的,而不是当前项目的 csproj 文件中,以便自动包含。

所有文件(如 cs 文件)都可以正常工作,但是,我不明白如何使用此 API 为构建创建一个子级。

目前,我的代码将文件添加到构建中,但 xaml cs 和 xaml 已连接但在视觉上是分开的(xaml cs 文件不在 xaml 文件内)

我的代码如下:(input.FolerPath是解决路径)

public void Execute(GeneratorInput input)
{
    var p = new Microsoft.Build.Evaluation.Project(input.FolderPath + ".csproj");
    p.AddItem("Compile", "SomePath.cs"); // this works given a proper path
    p.AddItem("Compile", "XamlCsPath.xaml.cs"); // this works for the xaml cs
    p.AddItem("Page", "XamlPath.xaml"); // this creates the xaml file seperately
}

最终,我想要实现的是以下csproj文件:

<Compile Include="XamlCsFile.xaml.cs"> // I got this far
    <DependentUpon>XamlFile.xaml</DependentUpon> // this is what I am trying to add
</Compile>

【问题讨论】:

标签: c# wpf xaml csproj addchild


【解决方案1】:

这似乎可以解决问题:

var element = p.AddItem("Compile", "XamlCsPath.xaml.cs"); // this works for the xaml cs
element[0].SetMetadataValue("DependentUpon", "XamlFile.xaml");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-14
    • 1970-01-01
    相关资源
    最近更新 更多