当 VS 设计器已经使用DebuggerNonUserCode属性实现它时,如何在 UserControl 上实现 Dispose(boolean) ?我对这个方法的修改会被删除吗?
(来自 UserControl.Designer.vb 的代码)
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Run Code Online (Sandbox Code Playgroud) 在库中创建 UserControl 然后将其放入 Form 项目中时,任何人都遇到问题...更改此控件的例如 BackColor 和重建,更改未反映在 Form 项目中?
已编辑:更新控件时,例如更改控件,将新控件放入控件内,更新正在发生。看起来控件背景颜色保持不变,或者只更改一次。
这太蹩脚了,我很累试图找出问题所在!
我使用 VS2010 Ultimate,Windows 7 x64。
我发现在删除控件时,添加了有关背景色的设计器初始化。如果我删除它,它会起作用。那有多糟糕?对我来说它看起来像一个错误?是吗?
你只能这样做一次!
我想绘制一个字符串并以自定义角度旋转它。简单地说,我计算包含旋转图像的区域的新尺寸,然后创建一个位图对象,并通过它的图形对象,我绘制一个具有 3 次变换(平移到中心、旋转和反向平移)的字符串。我编写了以下代码,但质量不理想。有没有人有想法?
private Image RotateText(string Text, int FontSize, float Angle)
{
//Modify angle
Angle *= -1;
//Calculate rotation angle in radian
double AngleInRadian = (Angle * 2 * Math.PI) / 360d;
//Instantiate a font for text
Font TextFont = new Font("Tahoma", FontSize, FontStyle.Bold);
//Measure size of the text
Graphics Graphic = this.CreateGraphics();
SizeF TextSize = Graphic.MeasureString(Text, TextFont);
//Calculate size of the rotated text
double NewWidth = Math.Abs(TextSize.Width * Math.Cos(AngleInRadian)) + Math.Abs(TextSize.Height * Math.Sin(AngleInRadian));
double NewHeight = Math.Abs(TextSize.Width * Math.Sin(AngleInRadian)) …Run Code Online (Sandbox Code Playgroud) 我正在使用 MVVM 模式在 WPF/c# 中编写表单并尝试与用户控件共享数据。(好吧,用户控件视图模型)
我要么需要:
用户控件仅用于使大型表单更易于管理,所以我不确定这是否是 MVVM 的方式,这正是我过去所做的方式。
我想在 Xaml 中传递一个 VM 构造的类。
<TabItem Header="Applicants">
<Views:ApplicantTabView>
<UserControl.DataContext>
<ViewModels:ApplicantTabViewModel Client="{Binding Client} />
</UserControl.DataContext>
</Views:ApplicantTabView>
</TabItem>
Run Code Online (Sandbox Code Playgroud)
<TabItem Header="Applicants">
<Views:ApplicantTabView>
<UserControl.DataContext>
<ViewModels:ApplicantTabViewModel Client="{Binding Client} />
</UserControl.DataContext>
</Views:ApplicantTabView>
</TabItem>
Run Code Online (Sandbox Code Playgroud)
但我似乎无法获得接受非静态内容的依赖属性。
这对我来说已经有一段时间了,但假设我会发现但失败了,所以我在这里。
提前致谢,奥利
我制作了两个用户控件并将其包含在我的主用户控件中。之后我在我的一个表单中使用了该用户控件,但现在的问题是它向我显示了无法创建实例的错误。当我将鼠标悬停在主窗体中的用户控件引用上时,它会显示“值不能为空参数名称:g”。给出错误的用户控件位于 Windows 的第 40 行,特别是<pc:SearchProduct x:Name="ProductFilterResult" Margin="0,0,0,0"/>。任何帮助将不胜感激
用户控件:
<UserControl x:Class="CPOSApplication.UserControls.Products.SearchProduct"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:uc="clr-namespace:CPOSApplication.UserControls.Products"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="500" Width="Auto" Padding="5">
<Grid Focusable="True" x:Name="ProductGrid" Loaded="ProductGrid_Loaded">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition Width="Auto" MinWidth="194" />
</Grid.ColumnDefinitions>
<Border Grid.ColumnSpan="2" Style="{DynamicResource SearchGridHeaderBorder}">
<DockPanel Grid.Row="0">
<Label x:Name="GridLabel" Style="{DynamicResource HeadingLabelsCustomStyle}" Content="Search Product:"/>
<Button x:Name="AddBtn" Style="{DynamicResource AddButtonCustomStyle}" Content="Add Product (F10)"/>
</DockPanel>
</Border>
<uc:ProductFilterResult x:Name="productFilterResult" Grid.Column="0" Grid.Row="1" HorizontalAlignment="Left" Margin="5,0"/>
<uc:FilterProduct x:Name="filterProduct" Grid.Column="1" Grid.Row="1" Width="300" Padding="2,0,0,0" Margin="5,0"/>
</Grid>
</UserControl>
Run Code Online (Sandbox Code Playgroud)
用户控件.CS文件
using …Run Code Online (Sandbox Code Playgroud) MySpecialView是一个复杂的图像控件,我想从不同的角度重用它,并ViewModel像本例一样传递它。
MainWindow.xaml
<Window x:Class="YouBug.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:YouBug"
mc:Ignorable="d"
DataContext="{Binding MainViewModel}"
Title="MainWindow" Height="350" Width="525">
<Grid>
<local:MySpecialView ViewModel="{Binding MySpecialViewModel}"></local:MySpecialView>
</Grid>
Run Code Online (Sandbox Code Playgroud)
主视图模型
public class MainViewModel
{
public MySpecialViewModel MySpecialViewModel { get; set; }
public MainViewModel()
{
MySpecialViewModel = new MySpecialViewModel();
//gets not displayed!
Task.Run(() => MySpecialViewModel.changeImage(5000, "C:\\Users\\user\\Pictures\\Capture.PNG"));
}
}
Run Code Online (Sandbox Code Playgroud)
MySpecialView.xaml
<UserControl x:Class="YouBug.MySpecialView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:YouBug"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<Image Source="{Binding ImageSource}" />
</Grid>
Run Code Online (Sandbox Code Playgroud)
MySpecialView.xaml.cs
public partial class MySpecialView : UserControl
{
public static readonly DependencyProperty …Run Code Online (Sandbox Code Playgroud) 我总是为空
_AdornerLayer = AdornerLayer.GetAdornerLayer(InteractiveCanvas);
Run Code Online (Sandbox Code Playgroud)
可能是什么原因。我想获取AdornerLayer并需要添加_AdornerCursor相同的
我看到了一些可能的答案,但对我没有用
我有一个主要的应用程序,在这里有不同的边框按钮。这也包括“ Einstellung”按钮。按下此按钮将显示与以前不同的内容。这是通过UserControl实现的。
现在,您可以在此用户控件中进行设置并保存。
我在“设置”中所做的节省
并与守则
Vorschau.Properties.Settings.Default.BreiteBitmap = Int32.Parse(tbBreiteBitmap.Text);
Vorschau.Properties.Settings.Default.HoeheBitmap = Int32.Parse(tbHoeheBitmap.Text);
Vorschau.Properties.Settings.Default.AnzahlKoordinaten = Int32.Parse(tbAnzahlKoordinaten.Text);
Run Code Online (Sandbox Code Playgroud)
现在,我按下“ Speichern”按钮。他保存了我的设置。现在,我更改了UserControl,因此单击“ Vorschau”按钮,并向我显示新内容。如果我现在再次单击“ Einstellung”,则内容仍然存在,因为我已经将其存储了。
但是问题来了。我关闭该应用程序,然后再次打开它。现在,我单击文本框中的“ Einstellung”按钮,到处都是0。
事实并非如此,但过去的内容应该来自那里。
启动应用程序时,我会检索此内容。请参阅代码段。
public UCEinstellung()
{
InitializeComponent();
//Lade der Einstellungen
tbBreiteBitmap.Text = Vorschau.Properties.Settings.Default.BreiteBitmap.ToString();
tbHoeheBitmap.Text = Vorschau.Properties.Settings.Default.HoeheBitmap.ToString();
tbAnzahlKoordinaten.Text = Vorschau.Properties.Settings.Default.AnzahlKoordinaten.ToString();
}
Run Code Online (Sandbox Code Playgroud)
和
public MainWindow()
{
InitializeComponent();
MouseDown += Window_MouseDown;
einstellung.tbBreiteBitmap.Text = Vorschau.Properties.Settings.Default.BreiteBitmap.ToString();
einstellung.tbHoeheBitmap.Text = Vorschau.Properties.Settings.Default.HoeheBitmap.ToString();
einstellung.tbAnzahlKoordinaten.Text = Vorschau.Properties.Settings.Default.AnzahlKoordinaten.ToString();
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,这不起作用。希望您能告诉我错误所在。在没有usercontrol的其他应用程序中,此存储和检索正确。
UCCEinstellung
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using …Run Code Online (Sandbox Code Playgroud) 我有这个代码来比较两个控件标签值的值:
Protected Function GetLabelTextForTag(tagVal As String) As String
Dim CoName As String = ""
For Each cntrl As Control In Me.Controls
If TypeOf cntrl Is Label Then
If DirectCast(cntrl, Label).Tag Is tagVal Then
CoName = DirectCast(cntrl, Label).Text
Exit For
End If
End If
Next
Return CoName
End Function
Run Code Online (Sandbox Code Playgroud)
即使我到达这条线,但是:
If DirectCast(cntrl, Label).Tag Is tagVal Then
Run Code Online (Sandbox Code Playgroud)
...和Tag与tagVal("1")相同,未到达下一行 - "1"与"1"不同
不熟悉VB,我想也许是"问题".但是,我之所以使用"IS"开头的原因是因为当我第一次尝试这个时:
If DirectCast(cntrl, Label).Tag = tagVal Then
Run Code Online (Sandbox Code Playgroud)
...我得到了," 错误1选项Strict On禁止运算符'='的Object类型的操作数.使用'Is'运算符来测试对象标识. "
该函数由按钮单击的事件处理程序调用:
Private Sub Button1_Click( sender As Object, e As EventArgs) Handles …Run Code Online (Sandbox Code Playgroud) 在我的项目"teacher_gui_windows_form"中,我试图将我从工具箱中创建的userControl拖到我的表单"teacher_gui",但它给出了错误:"无法创建组件'StudentControl'.错误消息如下:System.MissingMethodException构造函数在类型'teacher_gui_windows_form.StudentControl'找不到."
但我确实有一个StudentControl构造函数.StudentControl类使用另一个表单,这是它的类代码:
public partial class StudentControl : UserControl
{
const int Num = 10;
Image image;
string message;
public StudentForm studentForm = null;
public StudentControl(Image image, string Ip, int index)
{
InitializeComponent();
this.image = image;
pictureBoxImage.Image = image;
labelIp.Text = Ip;
Location = new Point(index % Num * (Width + 5), index / Num * (Height + 5));
}
public void ChangeImage(Image image)
{
pictureBoxImage.Image = image;
}
private void StudentControl_Click(object sender, EventArgs e)
{
studentForm = new StudentForm(pictureBoxImage.Image);
studentForm.ShowDialog(); …Run Code Online (Sandbox Code Playgroud)