C# 自定义用户控件破坏了XAML Designerier和Intellisense
我正在开发一个Windows应用商店的通用应用程序,目前我主要关注WP8.1。我已经创建了一个自定义UserControl,但是每当我使用XAML在视图中包含这个自定义控件时,XAML设计器都无法加载设计视图。它只是显示“加载设计器…”并使用超过60%的CPU 通常我不会关心Designer视图,因为我更喜欢直接修改XAML。问题是,当我在编写XAML时依赖的设计器挂起时,我失去了智能感知 我的示例是一个地图控件,它可以在Windows Phone和Windows应用商店应用程序上运行,但我在其他更简单的控件上也遇到了这个问题 有趣的是,XAML设计器在直接处理UserControl时工作得很好。这只是在另一个视图的XAML中包含UserControl时的问题 我的UserControl XAML是:C# 自定义用户控件破坏了XAML Designerier和Intellisense,c#,xaml,visual-studio-2013,windows-phone-8.1,C#,Xaml,Visual Studio 2013,Windows Phone 8.1,我正在开发一个Windows应用商店的通用应用程序,目前我主要关注WP8.1。我已经创建了一个自定义UserControl,但是每当我使用XAML在视图中包含这个自定义控件时,XAML设计器都无法加载设计视图。它只是显示“加载设计器…”并使用超过60%的CPU 通常我不会关心Designer视图,因为我更喜欢直接修改XAML。问题是,当我在编写XAML时依赖的设计器挂起时,我失去了智能感知 我的示例是一个地图控件,它可以在Windows Phone和Windows应用商店应用程序上运行,但我在其
<UserControl
x:Class="MyProject.Controls.MyMapControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:MyProject.Control"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">
<Grid>
<Image x:Name="image" Stretch="UniformToFill" />
</Grid>
</UserControl>
后面的代码看起来像
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.Devices.Geolocation;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using System.Threading.Tasks;
#if WINDOWS_PHONE_APP
using Windows.UI.Xaml.Controls.Maps;
using Windows.UI.Xaml.Shapes;
#elif WINDOWS_METRO_APP
using Bing.Maps;
#endif
// The User Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234236
namespace MyProject.Controls
{
public sealed partial class MyMapControl : UserControl
{
#if WINDOWS_PHONE_APP
private MapControl map;
#elif WINDOWS_METRO_APP
private Map map;
private MapShapeLayer shapeLayer;
#endif
/// <summary>
/// Creates the MyMapControl with the appropriate credentials
/// </summary>
public MyMapControl()
{
this.InitializeComponent();
#if WINDOWS_PHONE_APP
map = new MapControl();
map.MapServiceToken = "{map service token goes here}";
#elif WINDOWS_METRO_APP
map = new Map();
map.Credentials = "{credentials go here"};
shapeLayer = new MapShapeLayer();
map.ShapeLayers.Add(shapeLayer);
#endif
this.main_grid.Children.Add(map);
}
#if WINDOWS_PHONE_APP
public double Pitch {
get {
return map.Pitch;
}
set {
if(map.DesiredPitch != value) {
map.DesiredPitch = value;
OnPropertyChanged("Pitch");
}
}
}
public MapStyle Style {
get {
return map.Style;
}
set {
if(value != map.Style) {
map.Style = value;
OnPropertyChanged("Style");
}
}
}
public MapColorScheme ColorScheme {
get {
return map.ColorScheme;
}
set {
if(map.ColorScheme != value) {
map.ColorScheme = value;
OnPropertyChanged("ColorScheme");
}
}
}
private bool _ShowLocation = true;
public bool ShowLocation {
get {
return this._ShowLocation;
}
set {
if(this._ShowLocation != value) {
this._ShowLocation = value;
OnPropertyChanged("ShowLocation");
}
}
}
#endif
/// <summary>
/// Get or set the map's ZoomLevel
/// </summary>
public double ZoomLevel {
get {
return map.ZoomLevel;
}
set {
if(map.ZoomLevel != value) {
map.ZoomLevel = value;
OnPropertyChanged("ZoomLevel");
}
}
}
/// <summary>
/// Get or set the map's manipulation mode
/// </summary>
public Windows.UI.Xaml.Input.ManipulationModes ManipulationMode {
get {
return map.ManipulationMode;
}
set {
map.ManipulationMode = value;
}
}
/// <summary>
/// Get or set the map's center point
/// </summary>
public Geopoint Center {
get {
#if WINDOWS_PHONE_APP
return map.Center;
#elif WINDOWS_METRO_APP
return map.Center.ToGeopoint();
#endif
}
set {
#if WINDOWS_PHONE_APP
map.Center = value;
#elif WINDOWS_METRO_APP
map.Center = value.ToLocation();
#endif
OnPropertyChanged("Center");
}
}
/// <summary>
/// Get or set the map's credentials
/// </summary>
public string Credentials {
get {
#if WINDOWS_PHONE_APP
return map.MapServiceToken;
#elif WINDOWS_METRO_APP
return map.Credentials;
#endif
}
set {
if(!string.IsNullOrEmpty(value)) {
#if WINDOWS_PHONE_APP
map.MapServiceToken = value;
#elif WINDOWS_METRO_APP
map.Credentials = value;
#endif
OnPropertyChanged("Credentials");
}
}
}
/// <summary>
/// Get or set the map's display of traffic
/// </summary>
public bool ShowTraffic {
get {
#if WINDOWS_PHONE_APP
return map.TrafficFlowVisible;
#elif WINDOWS_METRO_APP
return map.ShowTraffic;
#endif
}
set {
#if WINDOWS_PHONE_APP
map.TrafficFlowVisible = value;
#elif WINDOWS_METRO_APP
map.ShowTraffic = value;
#endif
OnPropertyChanged("ShowTraffic");
}
}
/// <summary>
/// Set the current location for the map view as well as zoom level.
/// </summary>
/// <param name="center">BasicGeoposition for the center of the map view</param>
/// <param name="zoom">Zoom Level at which to display map</param>
public void SetView(BasicGeoposition center, double zoom) {
#if WINDOWS_PHONE_APP
map.Center = new Geopoint(center);
map.ZoomLevel = zoom;
#elif WINDOWS_METRO_APP
map.SetView(center.ToLocation(), zoom);
#endif
OnPropertyChanged("ZoomLevel");
OnPropertyChanged("Center");
}
/// <summary>
/// Adds a basic push pin with text to the map at a specific location.
/// </summary>
/// <param name="location">Location for pushpin</param>
/// <param name="text">Text to display with pushpin</param>
public void AddPushpin(BasicGeoposition location, string text) {
#if WINDOWS_PHONE_APP
Grid pin = new Grid() {
Width = 34,
Height = 24,
Margin = new Thickness(-12)
};
pin.Children.Add(new Ellipse() {
Fill = new SolidColorBrush(Colors.DodgerBlue),
Stroke = new SolidColorBrush(Colors.White),
StrokeThickness = 3,
Width = 24,
Height = 24
});
pin.Children.Add(new TextBlock() {
Text = text,
FontSize = 12,
Foreground = new SolidColorBrush(Colors.White),
HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Center,
VerticalAlignment = Windows.UI.Xaml.VerticalAlignment.Center
});
MapControl.SetLocation(pin, new Geopoint(location));
map.Children.Add(pin);
#elif WINDOWS_METRO_APP
Pushpin pin = new Pushpin() {
Text = text
};
MapLayer.SetPosition(pin, location.ToLocation());
map.Children.Add(pin);
#endif
}
/// <summary>
/// Adds a polygon to the map
/// </summary>
/// <param name="locations"></param>
/// <param name="strokeColor"></param>
/// <param name="strokeThickness"></param>
public void AddPolygon(List<BasicGeoposition> locations, Color fillColor, Color strokeColor, double strokeThickness) {
#if WINDOWS_PHONE_APP
MapPolygon line = new MapPolygon() {
Path = new Geopath(locations),
StrokeColor = strokeColor,
StrokeThickness = strokeThickness
};
map.MapElements.Add(line);
#elif WINDOWS_METRO_APP
MapPolygon line = new MapPolygon() {
Locations = locations.ToLocationCollection(),
FillColor = fillColor
};
shapeLayer.Shapes.Add(line);
#endif
}
/// <summary>
/// Clears all elements on the map
/// </summary>
public void ClearMap() {
#if WINDOWS_PHONE_APP
map.MapElements.Clear();
#elif WINDOWS_METRO_APP
shapeLayer.Shapes.Clear();
#endif
map.Children.Clear();
}
/// <summary>
/// Notify Property Changed Event
/// </summary>
public event DependencyPropertyChangedEventHandler PropertyChanged;
/// <summary>
/// Notify Property Changed method
/// </summary>
/// <param name="propertyName">Name of modified property</param>
internal void OnPropertyChanged(string propertyName) {
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.IO;
使用System.Linq;
使用System.Runtime.InteropServices.WindowsRuntime;
使用Windows基金会;
使用Windows。
使用Windows.UI;
使用Windows.UI.Xaml;
使用Windows.UI.Xaml.Controls;
使用Windows.Devices.Geolocation;
使用Windows.UI.Xaml.Controls.Primitives;
使用Windows.UI.Xaml.Data;
使用Windows.UI.Xaml.Input;
使用Windows.UI.Xaml.Media;
使用Windows.UI.Xaml.Navigation;
使用System.Threading.Tasks;
#如果WINDOWS\u PHONE\u应用程序
使用Windows.UI.Xaml.Controls.Maps;
使用Windows.UI.Xaml.Shapes;
#elif WINDOWS\u METRO\u应用程序
使用Bing.Maps;
#恩迪夫
//用户控制项模板记录在http://go.microsoft.com/fwlink/?LinkId=234236
命名空间MyProject.Controls
{
公共密封部分类MyMapControl:UserControl
{
#如果WINDOWS\u PHONE\u应用程序
私有地图控制地图;
#elif WINDOWS\u METRO\u应用程序
私人地图;
私有映射shapeLayer shapeLayer;
#恩迪夫
///
///使用适当的凭据创建MyMapControl
///
公共MyMapControl()
{
this.InitializeComponent();
#如果WINDOWS\u PHONE\u应用程序
map=新的MapControl();
map.MapServiceToken=“{map service token位于此处}”;
#elif WINDOWS\u METRO\u应用程序
map=新map();
map.Credentials=“{Credentials go here”};
shapeLayer=新映射shapeLayer();
map.shapeLayer.Add(shapeLayer);
#恩迪夫
this.main_grid.Children.Add(map);
}
#如果WINDOWS\u PHONE\u应用程序
公共双音高{
得到{
返回地图。俯仰;
}
设置{
if(map.DesiredPitch!=值){
map.DesiredPitch=值;
不动产变更(“沥青”);
}
}
}
公共地图样式{
得到{
返回地图。风格;
}
设置{
if(值!=map.Style){
map.Style=值;
OnProperty变更(“样式”);
}
}
}
公共地图配色方案配色方案{
得到{
返回map.ColorScheme;
}
设置{
if(map.ColorScheme!=值){
map.ColorScheme=值;
OnPropertyChanged(“配色方案”);
}
}
}
私有bool_ShowLocation=true;
公共场所{
得到{
返回此地址。\u ShowLocation;
}
设置{
如果(此._ShowLocation!=值){
这。_ShowLocation=值;
OnPropertyChanged(“ShowLocation”);
}
}
}
#恩迪夫
///
///获取或设置地图的缩放级别
///
公共双ZoomLevel{
得到{
返回map.ZoomLevel;
}
设置{
if(map.ZoomLevel!=值){
map.ZoomLevel=值;
OnPropertyChanged(“ZoomLevel”);
}
}
}
///
///获取或设置贴图的操作模式
///
公共Windows.UI.Xaml.Input.OperationModes操作模式{
得到{
返回map.manufactionMode;
}
设置{
map.operationmode=值;
}
}
///
///获取或设置地图的中心点
///
公共地质点中心{
得到{
#如果WINDOWS\u PHONE\u应用程序
返回地图中心;
#elif WINDOWS\u METRO\u应用程序
返回map.Center.togepoint();
#恩迪夫
}
设置{
#如果WINDOWS\u PHONE\u应用程序
地图中心=价值;
#elif WINDOWS\u METRO\u应用程序
map.Center=value.ToLocation();
#恩迪夫
不动产变更(“中心”);
}
}
///
///获取或设置映射的凭据
///
公共字符串凭据{
得到{
#如果WINDOWS\u PHONE\u应用程序
返回map.MapServiceToken;
#elif WINDOWS\u METRO\u应用程序
返回map.Credentials;
#恩迪夫
}
设置{
如果(!string.IsNullOrEmpty(值)){
#如果WINDOWS\u PHONE\u应用程序
map.MapServiceToken=值;
#elif WINDOWS\u METRO\u应用程序
map.Credentials=值;
#恩迪夫
OnPropertyChanged(“凭证”);
}
}
}
///
///获取或设置交通地图的显示