我正在尝试进行自定义验证[IsUnique].检查属性值是否唯一并返回正确的消息.
这是我的代码,但这只适用于指定的类,是否可以通过元数据来获取正确的类?
public class ArticleMetaData
{
[Required(AllowEmptyStrings = false)]
[IsUnique("Name")]
public String Name{ get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我的自定义验证:
class IsUnique : ValidationAttribute
{
public IsUnique(string propertyNames)
{
this.PropertyNames = propertyNames;
}
public string PropertyNames { get; private set; }
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
var myproperty = validationContext.ObjectType.GetProperty(PropertyNames);
var value = propiedad.GetValue(validationContext.ObjectInstance, null);
IEnumerable<String> properties;
List<string> propertiesList = new List<string>();
propertiesList.Add(myproperty.Name);
var dba = new myContext();
if (dba.Articles.Any(article => article.Name == (string)value))
{
return new ValidationResult("The name …Run Code Online (Sandbox Code Playgroud) 我正在尝试将可点击的图像/按钮添加到datagridview按钮列.
图像/按钮将是播放或停止的图标.如果用户单击播放按钮,则启动系统上的服务,如果用户单击停止按钮,则服务停止.
我已经编写了启动和停止服务的函数.我遇到的困难是让按钮/图像显示在数据网格中并使其可点击.
这是我对代码的看法:
this.dgrdServices.RowPrePaint +=new DataGridViewRowPrePaintEventHandler(dgv_RowPrePaint);
this.dgrdServices.Rows.Add();
this.dgrdServices.Rows[0].Cells[0].Value = Image.FromFile(@"C:\users\brad\desktop\green-dot.gif");
this.dgrdServices.Rows[0].Cells[1].Value = "MyServer";
this.dgrdServices.Rows[0].Cells[2].Value = "MyService";
this.dgrdServices.Rows[0].Cells[3].Value = "Started";
this.dgrdServices.Rows[0].Cells[4].Value = new DataGridViewButtonCell();
this.dgrdServices.Rows[0].Cells[5].Value = "Uninstall";
Run Code Online (Sandbox Code Playgroud)
如果最好使用一个图像按钮或一个可点击的图像,我就无法解决.我也无法正确显示按钮.
谢谢Brad
我有以下代码:
public class OurTextBox : TextBox
{
public OurTextBox()
: base()
{
this.SetStyle(ControlStyles.UserPaint, true);
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Pen penBorder = new Pen(Color.Gray, 1);
Rectangle rectBorder = new Rectangle(e.ClipRectangle.X, e.ClipRectangle.Y, e.ClipRectangle.Width - 1, e.ClipRectangle.Height - 1);
e.Graphics.DrawRectangle(penBorder, rectBorder);
}
}
Run Code Online (Sandbox Code Playgroud)
这是完美的,但它没有显示文本,直到它得到焦点.
有谁能够帮我?怎么了?
预先感谢.
我正在尝试从 url (例如“myapp://somestring”)启动我的 C# 应用程序,并且我已经能够做到这一点,但是我仍然无法理解如何读取该 url 的“somestring”值应该传递给应用程序。
我尝试了以下代码,但没有任何结果:
static void Main (string[] args){
foreach (string arg in args) {
Console.Writeline(arg);
}
}
Run Code Online (Sandbox Code Playgroud)
要知道,该应用程序是使用 xamarin for mac 完成的。
预先感谢您的任何帮助
我的应用程序上有一个ActionFilterAttribute,用于在检测到用户未经过身份验证时重定向用户.在过滤器中,我想检测一个动作的ReturnType何时是JsonResult.
作为一种解决方法,我最初创建了IsJsonResult的自定义属性,并使用该属性修饰了我的解决方案中的JsonResult方法.这有效,并在动作过滤器中实现如下:
public class CheckUser : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext actionExecutingContext)
{
base.OnActionExecuting(actionExecutingContext);
object[] customAttributes = actionExecutingContext.ActionDescriptor.GetCustomAttributes(true);
bool isJsonResult = customAttributes.FirstOrDefault(a => a.GetType() == typeof(IsJsonResult)) != null;
if (isJsonResult)
{
return; // Don't perform any additional checks on JsonResult requests.
}
// Additional checking code omitted.
}
}
Run Code Online (Sandbox Code Playgroud)
这有效,但我不喜欢在这个项目中装饰所有JsonResult动作的想法.如果将新的JsonResult添加到项目中,那很容易失败,我们忘记相应地装饰它.
此外,我可以看到名称为"JsonResult"的ReturnType位于上面显示的actionExecutingContext对象中的调试器中.以下是观察窗口中显示的路径:
actionExecutingContext > ActionDescriptor > [System.Web.Mvc.ReflectedActionDescriptor] > MethodInfo > ReturnType > FullName
Run Code Online (Sandbox Code Playgroud)
该FullName属性的值为"System.Web.Mvc.JsonResult".
所以我似乎可以直接从actionExecutingContext对象中提取该值,并创建一个支持方法来返回一个bool指标.为此,这里是我编写的代码.
private bool isReturnTypeJson(ActionExecutingContext actionExecutingContext)
{
string actionName = actionExecutingContext.ActionDescriptor.ActionName;
string controllerName = actionExecutingContext.ActionDescriptor.ControllerDescriptor.ControllerName;
Type controllerType …Run Code Online (Sandbox Code Playgroud) 有两种形式.Form2源自Form1.
但是我在设计模式下遇到了Form2的问题,如下面的屏幕截图所示.
如果我对此发表评论this._presenter.Retrive();,它将正常工作.是怎么回事以及如何解决这个问题?
UPD: 如果我将删除throw new NotImplementedException(); 并将插入,例如,MessageBox.Show("Test");,每次我打开Form2时,MessageBox都会出现,就像我运行应用程序一样.
窗体2
namespace InheritanceDemo
{
public partial class Form2 : Form1
{
public Form2()
{
InitializeComponent();
}
}
}
Run Code Online (Sandbox Code Playgroud)
Form1中
namespace InheritanceDemo
{
public partial class Form1 : Form
{
protected IPresenter _presenter;
public Form1()
{
InitializeComponent();
_presenter = new Presenters();
}
private void Form1_Load(object sender, EventArgs e)
{
this._presenter.Retrive();
}
}
public class Presenters : IPresenter
{
public void Retrive()
{
throw new NotImplementedException(); …Run Code Online (Sandbox Code Playgroud) 我想DataGrid在MouseDown活动中获取 a 的点击单元格。到目前为止我还没有成功。
我试过
dg.CurrentCell- 包含先前单击的单元格
dg.GetChildAt(...)- 不起作用,因为单元格未列为 DataGrid 的子级
Windows 10 的最新主要更新“Fall Creators Update”(又名 RedStone3)添加了可在任何文本框中使用的系统范围表情符号弹出窗口的功能。
我正在尝试制作一个程序,当单击按钮时会启动相同的弹出表情符号窗口。正如在另一个关于类似主题的讨论中所建议的那样,我尝试使用InputSimulator项目。还有其他方法,正如这里建议的那样,但似乎模拟器很好地包装了它们。
我所做的只是创建一个新的小型 WPF 应用程序,其中有一个主窗口,其中包含一个 TextBox 和一个按钮。按下按钮将运行以下代码:
textBox.Focus()
new InputSimulator().Keyboard.ModifiedKeyStroke(VirtualKeyCode.LWIN, VirtualKeyCode.OEM_PERIOD);
Run Code Online (Sandbox Code Playgroud)
这似乎没有影响!我也试过 OEM_1(这是“:;”键码)而不是 OEM_PERIOD,但仍然没有运气。问题是,LWIN 与另一个键(例如 VK_P)的任何其他组合都可以使用相同的模拟器代码。
如果我尝试按下真实键盘上的表情符号热键,在运行该代码后,第一次按下什么都不做(有时表情符号弹出窗口会显示半秒钟,然后立即消失),然后需要再次按顺序按下热键用于显示弹出窗口。这让我怀疑弹出窗口是否显示,仅在屏幕边界之外,或者等待另一个键盘事件发生/完成?
我无法让WinForms RichTextBox显示一些Unicode字符,特别是数学字母数字符号(但问题很可能不仅限于那些).
令人惊讶的是,相同的字符可以TextBox使用相同(默认)字体以普通或多行显示.即使我将字体更改为例如"Arial"或"Lucida",我也会得到相同的结果.
屏幕截图来自Windows 10,但我在Windows 7上得到了相同的结果.该示例显示了ascii小广告,后跟数学斜体sans-serif小alpha-delta.
我正在使用Visual Studio 2017和.NET 4.6.1.
一个简单的测试代码:
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
// ...
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(25, 38);
this.textBox1.Multiline = true;
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(182, 108);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "abcd ";
//
// richTextBox1
//
this.richTextBox1.Location = new System.Drawing.Point(213, 38);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(179, 108);
this.richTextBox1.TabIndex …Run Code Online (Sandbox Code Playgroud) 我想执行数据绑定,Room.ToNorth.ImageName而while ToNorth可能为null.
我正在尝试编写自己的文本冒险,并且在玩家可以前往的每个方向旁边都有图像(即打开/关闭的门,通道).当在给定方向上没有房间时,我有控件绑定显示"无退出"图像,这很有效,所以玩家起始位置在所有4个方向都有一个出口,但是当一个为空时,我得到了以下例外
System.ArgumentNullException:'值不能为null.参数名称:component'
这不是一个新游戏的问题,但我正在制作随机生成的地下城,所以在加载游戏中无法保证.我意识到我可以通过创建一个安全的空间,然后设置绑定,然后将播放器移动到他们需要的位置,但有没有一个正确的方法来处理它?
我找到的最接近的答案是这个问题,但我不确定我是否可以在这里应用它,因为它受到了限制.
private GameSession _CurrentGame;
private BindingSource _BindToPlayer = new BindingSource();
private void BtnNewGame_Click(object sender, EventArgs e)
{
_CurrentGame = new GameSession();
_BindToPlayer.DataSource = _CurrentGame.CurrentPlayer;
PicBoxNorth.DataBindings.Add("ImageLocation", _BindToPlayer,
"CurrentRoom.ToNorth.ImageName", true, DataSourceUpdateMode.OnPropertyChanged,
"Images\\Wall.png");
}
Run Code Online (Sandbox Code Playgroud)
ToNorth属性只是从一个出口数组中获取对象,但如果它为null(如上面的链接中所示),则告诉它返回一个空出口将会出现问题,因为出口没有任何连接的房间和因此无法初始化可能会在此过程中抛出异常.为了更好地解释,我的房间设置如下
public Class Room
{
public int ID { get; set; }
public String Name { get; set; }
public String Description { get; set; }
public String FarDescription { get; set; }
public CoOrds Co_Ords { get; set; …Run Code Online (Sandbox Code Playgroud) c# ×9
.net ×8
winforms ×7
asp.net ×1
asp.net-mvc ×1
data-binding ×1
datagrid ×1
datagridview ×1
emoji ×1
inheritance ×1
input ×1
macos ×1
richtextbox ×1
textbox ×1
unicode ×1
url-scheme ×1
validation ×1
vb.net ×1
wpf ×1
xamarin ×1