所以突然间,在我的所有解决方案中的每个 xaml.cs 类中都出现了常见错误“当前上下文中不存在名称 InitializeComponent”,我尝试了多种常见解决方案来解决这个问题,但似乎没有一个有效。
继承 Content 视图的 xaml.cs:
using System;
using System.Collections.Generic;
using Xamarin.Forms;
namespace FirstApp.Renderers
{
public partial class CustomExpansibleMenu : ContentView
{
public CustomExpansibleMenu()
{
//Error here
InitializeComponent();
}
}
}
从内容页面继承的 xaml.cs:
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using FirstApp.Services;
using Xamarin.Essentials;
using Xamarin.Forms;
namespace FirstApp.Pages
{
public partial class ChatMenuPage : ContentPage
{
private NavigationService navigation;
public static double numPixelsW;
public static double numPixelsH;
public ChatMenuPage()
{
numPixelsH = DeviceDisplay.MainDisplayInfo.Height / DeviceDisplay.MainDisplayInfo.Density;
numPixelsW = DeviceDisplay.MainDisplayInfo.Width / DeviceDisplay.MainDisplayInfo.Density;
navigation = new NavigationService();
//Error here
InitializeComponent();
//Another error here, searchBox is the name of a frame in my xaml page, but //I cant access to it because of the previous error
searchBox.BorderWith = 3;
}
}
}
这些是我尝试过的常见解决方案:
- 验证 xaml 的 buildAction 是否设置为 EmbeddedResource 2) 验证 xaml 和 xaml.cs 中的命名空间是否相同。
- 在 xaml 中验证自定义工具:MSBuild:UpdateDesignTimeXaml