小编unk*_*656的帖子

C#ShowDialog Parent Form为null

我有两种形式.

LoadWorkstationFile提示他们要加载的ID的用户.

DisplayDataForm显示ID的数据,他们选择了以前屏幕上.

DisplayDataForm中,他们可以单击一个选项来加载新数据,该数据将LoadDataForm称为ShowDiaglog:

private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
  var answer = MessageBox.Show("Do you wish to save the current work file before continuing?", "Confirmation",
                               MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
  if (answer == DialogResult.Cancel)
    return;
  if (answer == DialogResult.Yes)
    SaveWorkFile();
  var prevworkstationid = Configuration.WorkstationId;
  var lw = new LoadWorkstationFile();
  lw.ShowDialog(this);
  if (Configuration.WorkstationId != prevworkstationid)
  {
    LoadData();
  }

}
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,我使用与之前相同的屏幕再次提示他们.

LoadWorkstationFile中,它具有以下代码:

  if (this.Parent == null)
  {
    var sc = new ScanCheck();
    sc.Show();
    this.Hide();
  }
Run Code Online (Sandbox Code Playgroud)

初始加载一切都很好.当我想再次加载数据时它已加载,我最终得到2个 …

c# forms

8
推荐指数
1
解决办法
1万
查看次数

中断文本框中的单词(使用TextWrapping = Wrap)

我不知道如何描述我的问题,但想象在WPF中使用长文本的TextBox.我设置TextWrapping="Wrap"为阻止整个字符串显示在一行,但我希望我的刺痛显示如下:

Lorem ipsum dolor sit amet, consectetur adipiscing el
it. Fusce ligula nulla, cursus finibus mauris vel, rh
oncus blandit sem. Fusce fermentum sed sem a porttito
r. Proin id convallis ex.
Run Code Online (Sandbox Code Playgroud)

而不是这个:

Lorem ipsum dolor sit amet, consectetur adipiscing
elit. Fusce ligula nulla, cursus finibus mauris vel,
rhoncus blandit sem. Fusce fermentum sed sem a
porttitor. Proin id convallis ex.
Run Code Online (Sandbox Code Playgroud)

区别在于,第一个文本在每n个字符后面都有一个"硬切" - 第二个文本被包装,每行不超过n个字符的长度


我是否必须\n在每 n 字符后插入一个,或者是否有WPF属性,这可以为我解决这个问题?


非常感谢你,祝大家圣诞快乐:)

c# wpf textbox word-wrap

8
推荐指数
1
解决办法
366
查看次数

模式匹配F#中的函数

我有一个可能不寻常的问题,但是如何使用模式匹配匹配F#中的函数?

想象一下:
我有多个功能签名,将多次使用,如:

binary function:  int -> int -> int
unary function:   int -> int
boolean function: int -> int -> bool
...
Run Code Online (Sandbox Code Playgroud)

现在想象一下这个函数evaluate,它本身就是一个函数f.签名f 必须是上面列出的一个.我如何匹配这种情况?


我尝试了以下方法:
测试No.1:使用代表和工会:

type UnaryFunction = delegate of int -> int
type BinaryFunction = delegate of (int -> int) -> int
type BooleanFunction = delegate of (int -> int) -> bool

type Functions =
    | Unary of UnaryFunction
    | Binary of BinaryFunction
    | Boolean of BooleanFunction

// …
Run Code Online (Sandbox Code Playgroud)

f# function pattern-matching

6
推荐指数
1
解决办法
501
查看次数

正则表达式 - 匹配任何重复 n 次的序列


你知道如何匹配一个重复n次的组吗?例如:

测试       [incl. the spaces between two 'is']
Foo bar
电话:+49 1 88 / 123 45 45 45

我尝试了以下正则表达式模式:(\w+)\1+
但它只匹配一组中的两次出现(而不是n次出现)

非常感谢

regex

5
推荐指数
1
解决办法
1037
查看次数

如何从 Visual Studio 项目中排除文件/文件夹?

我找不到任何选项或命令来将文件或文件夹从我的 Visual Studio 项目中排除。(csproj、jsproj ...)

可以选择在解决方案资源管理器中包含文件和文件夹 -> 显示所有文件 ->在目标上单击鼠标左键-> 包含在项目中。

但没有排除的选项...

我用的是VS 2015。

我注意到 VS 2008 和 2010 中提供了排除选项。通过此文档:

https://msdn.microsoft.com/en-us/library/0ebzhwsk(v=vs.100).aspx

有什么解决办法吗?

谢谢

编辑:

问题仅出现在jsproj类型的项目中(例如 Apache Cordova 项目)。Exuding 在csproj中运行得很好。

javascript c# csproj visual-studio-cordova visual-studio-2015

5
推荐指数
2
解决办法
7883
查看次数

通过反射获取指针值

我有一个类型的实例object,从中我知道它是一个指针(可以很容易地用 验证myobject.GetType().IsPointer)。是否可以通过反射获取指针的值?

到目前为止的代码:

object obj = .... ; // type and value unknown at compile time
Type t = obj.GetType();

if (t.IsPointer)
{
    void* ptr = Pointer.Unbox(obj);

    // I can obtain its (the object's) bytes with:
    byte[] buffer = new byte[Marshal.SizeOf(t)];
    Marshal.Copy((IntPtr)ptr, buffer, 0, buffer.Length);

    // but how can I get the value represented by the byte array 'buffer'?
    // or how can I get the value of *ptr?
    // the following line obviously doesn't work: …
Run Code Online (Sandbox Code Playgroud)

c# reflection pointers

5
推荐指数
1
解决办法
1243
查看次数

F#功能添加

我想在F#中实现一个(简单的)数学函数加法,意思是:

想象一下F是所有函数的字段,它将A的元素映射到B的元素: enter image description here
然后,我的"功能添加"应定义如下: enter image description here

我尝试了以下代码来实现函数添加作为运算符!+:

let inline (!+) (f1 : ^a -> ^b, f2 : ^a -> ^b) : ^a -> ^b =
    let f (x : ^a) : ^b =
        (f1 x) + (f2 x)
    f
Run Code Online (Sandbox Code Playgroud)

但是,如果我想编译以下行,我将收到一个错误:

let f1 x : float = -x // negate x
let f2 x : float = 2. * x // multiply by 2
let f3 = f1 !+ f2 //error : Expexceted `float`, got `'a …
Run Code Online (Sandbox Code Playgroud)

math f# functional-programming

5
推荐指数
1
解决办法
149
查看次数

在(字典)索引器上使用C#的ref功能

我想知道是否可以使用C#的ref returnon(字典)索引器或定义a setgetaccessor的属性,例如:

readonly Dictionary<string, int> dictionary = ...;


ref int v = ref dictionary["foo"];
//          ^^^^^^^^^^^^^^^^^^^^^
// CS0206: A property or indexer may not be passed as an out or ref parameter
v = 42;
Run Code Online (Sandbox Code Playgroud)

是否有可能以某种方式为ref属性或索引器提供功能(不使用反射)?如果是这样,怎么样?


我知道,错误信息在这个意义上是明确的 - 但是,我想知道哪个是实现其语义的最佳方式.

c# ref

5
推荐指数
1
解决办法
249
查看次数

使用 Reflection.Emit 动态发出 `[MarshalAs(...)]`

我需要[MarshalAs(UnamangedType.LPWStr)]在运行时使用System.Reflection.Emit. 这需要我创建一个继承类型MulticastDelegate(目前没有问题)、一个适当的构造函数和Invoke带有特殊编组参数的-method。

到目前为止我的 C# 代码:

TypeBuilder type_builder = ...;

MethodBuilder method_builder = type_builder.DefineMethod(
    "Invoke",
    MethodAttributes.NewSlot | MethodAttributes.Virtual | MethodAttributes.HideBySig | MethodAttributes.Public,
    CallingConventions.Standard,
    typeof(int),
    new[] { typeof(string) } // only an example - this array could hold any number of parameter types
);
ParameterBuilder parameter_1 = invoke.DefineParameter(1, ParameterAttributes.HasFieldMarshal, "arg0");

parameter_1.SetCustomAttribute(new CustomAttributeBuilder(
    typeof(MarshalAsAttribute).GetConstructor(new[] { typeof(UnmanagedType) }),
    new object[] { UnmanagedType.LPWStr }
));

// ...

type_builder.CreateType();
Run Code Online (Sandbox Code Playgroud)

我可以生成动态程序集,但是由于无效的 IL 指令,应用程序在调用“Invoke”方法时崩溃。

在使用我检测到的peverifyildasm-tools进行仔细检查后,我上面的代码发出以下 IL: …

.net c# cil reflection.emit marshalling

5
推荐指数
1
解决办法
64
查看次数

由于"歧义",F#通用约束不起作用

我有一个应用程序,它呈现钢琴表,因此我必须将一些乐谱概念抽象为记录结构.为了节省一些输入,我有时会将成员添加FromTuple到某些记录类型中.
我还介绍了运算符!>,它接受一个元组并返回适当的元组.但是,我有以下问题:

FS0332:无法解决在此程序点或附近使用运算符'FromTuple'时固有的模糊性.考虑使用类型注释来解决歧义.

我找不到我的错误的实际来源(我首先想到一些记录字段名称可能在多种记录类型中定义/使用,但似乎并非如此).


类型定义:

type xyz =
    {
        // some record fields
        Field1 : int
        Field2 : bool
        Field3 : string * string
    }
    with
        static member FromTuple (a, b, c) = { Field1 = a; Field2 = b; Field3 = c }

// more types defined like `xyz`

[<AutoOpen>]
module Globals =
    let inline (!>) x = (^b : (static member FromTuple : ^a -> ^b) x)
Run Code Online (Sandbox Code Playgroud)

故障线(在单独的文件中):

//ERROR
let my_xyz : xyz = …
Run Code Online (Sandbox Code Playgroud)

generics f# constraints operator-overloading

4
推荐指数
1
解决办法
99
查看次数