有没有理由为什么Visual Studio不允许我在DLL项目中创建WPF窗口?
我通过在Application Project中创建一个窗口并将其复制到我的DLL项目来"解决"它.我还发现我可以创建一个UserControl并将基类更改为"Window".
但如果我不得不这样做,那可能是因为我不应该这样做......
我尝试开始使用 .Net MAUI,并按照以下描述的步骤设置了我的开发环境:
我还运行了“maui-check”CLI 工具,一切都已检查完毕,但是当我使用 Visual Studio 2019 v16.11.0 Preview 2.0(在 Windows 10 Home 20H2 上运行)创建新的 .NET MAUI 应用程序时,我得到了“当前上下文中不存在名称“InitializeComponent”构建错误。它还找不到对表单上任何控件的引用,例如“当前上下文中不存在名称“CounterLabel””
我已经尝试了这篇文章中的几乎所有内容。名称“InitializeComponent”在当前上下文中不存在,其中包含添加和删除文件、进行更改以及将其改回等建议......基本上除了在许愿井中扔一分钱之外的所有内容。
我发现一个常见的错误是命名空间不匹配,但以下是我所显示的命名空间是正确的:
应用程序.xaml:
<Application xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:MauiApp1"
x:Class="MauiApp1.App">
...
</Application>
Run Code Online (Sandbox Code Playgroud)
应用程序.xaml.cs
using Microsoft.Maui;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.PlatformConfiguration.WindowsSpecific;
using System;
using Application = Microsoft.Maui.Controls.Application;
namespace MauiApp1
{
public partial class App : Application
{
public App()
{
InitializeComponent(); <-- This is throwing the build error...
}
protected override IWindow CreateWindow(IActivationState activationState)
{
this.On<Microsoft.Maui.Controls.PlatformConfiguration.Windows>()
.SetImageDirectory("Assets");
return new Microsoft.Maui.Controls.Window(new …Run Code Online (Sandbox Code Playgroud) 嗨,我收到一个错误,InitializeComponent在我的app.xaml.cs页面,我检查了网络,但一切,但没有解决方案的工作.请帮忙.
C#文件:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Newtonsoft.Json;
namespace Miser_sApp
{
public partial class App : Application
{
/// <summary>
/// Provides easy access to the root frame of the Phone Application.
/// </summary>
/// <returns>The root frame of the Phone Application.</returns>
public PhoneApplicationFrame RootFrame { get; private set; }
/// …Run Code Online (Sandbox Code Playgroud) 我开始学习XAML,并在BlankPage应用程序中添加了一些代码.突然间,一个构造函数正在初始化一个组件:
public BlankPage()
{
this.InitializeComponent();
}
Run Code Online (Sandbox Code Playgroud)
停止工作.我现在有这个错误:
"BlankApplication.BlankPage"不包含关于"的InitializeComponent"的定义和没有扩展方法"的InitializeComponent"接受类型"BlankApplication.BlankPage"的第一个参数可以找到(是否缺少using指令或程序集引用?)
老实说,我没有做任何事,我甚至没有看到这部分代码,现在它不起作用.
截图:
我最近将 .NET Framework 4.8 WPF 应用程序转换为 .NET5。当我编译应用程序时,出现以下错误
当前上下文中不存在名称“InitializeComponent”
我点击了这个链接,但帮不了我太多。
我检查了文件命名*.xaml空间*.cs。
<Window x:Class="Cl.Wpf.MessageWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
SizeToContent = "WidthAndHeight"
Title="MessageWindow" >
Run Code Online (Sandbox Code Playgroud)
还检查了.cs文件
namespace Cl.Wpf
{
public partial class MessageWindow : Window
{
public MessageWindow()
{
InitializeComponent();
.....
}
Run Code Online (Sandbox Code Playgroud)
我的*.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0-windows</TargetFramework>
<OutputType>Library</OutputType>
<RootNamespace>Cl.Wpf</RootNamespace>
<Deterministic>false</Deterministic>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<UseWPF>true</UseWPF>
<ImportWindowsDesktopTargets>true</ImportWindowsDesktopTargets>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.DotNet.UpgradeAssistant.Extensions.Default.Analyzers" Version="0.2.241603">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<Compile Update="Properties\Settings.Designer.cs">
<DesignTimeSharedInput>True</DesignTimeSharedInput>
<AutoGen>True</AutoGen>
<DependentUpon>Settings.settings</DependentUpon>
</Compile>
</ItemGroup>
<ItemGroup>
<None Update="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
</None>
</ItemGroup>
</Project> …Run Code Online (Sandbox Code Playgroud) 今天我遇到了这个问题,我想在我现有的类库项目中添加一个WPF窗口,但是“添加新项”列表中不存在该窗口。
我找到了一个解决方案,只想将其发布在这里,因为我在其他地方找不到它。