小编Lam*_*fif的帖子

当用户控件的内容更改时引发事件

我有一个 WPF 应用程序,其中有此用户控件

看法

<Grid  Background="{StaticResource ResourceKey=baground}" >
        <DockPanel >
            <UserControl x:Name="container"   ></UserControl>
        </DockPanel>
    </Grid>
Run Code Online (Sandbox Code Playgroud)

我需要添加一个在内容container更改时引发的事件:

 private void container_LayoutUpdated_1(object sender, EventArgs e)
        {
            Thread windowThread2 = new Thread(delegate() { verifing2(); });
            windowThread2.SetApartmentState(ApartmentState.STA);
            windowThread2.IsBackground = true;
            windowThread2.Start();

            Thread windowThread3 = new Thread(delegate() { verifing3(); });
            windowThread3.SetApartmentState(ApartmentState.STA);
            windowThread3.IsBackground = true;
            windowThread3.Start();
        }
Run Code Online (Sandbox Code Playgroud)

正如你所看到的,我尝试了这个活动LayoutUpdated,但我认为这不是最好的主意,因为它被提出了很多次,即使container内容没有改变。

  • 我该如何解决这个问题?

谢谢,

.net c# wpf user-interface multithreading

3
推荐指数
1
解决办法
1728
查看次数

C#应用程序中的Environment.FailFast

我想了解Environment.FailFastc#应用程序中的规则.所以,我做了这个代码:

  public static void Main()
   {
       string strInput = GetString();
       Console.WriteLine(strInput);
       Console.ReadKey();
   }

   private static string GetString()
   {
       Console.WriteLine("Get string");
       string s = Console.ReadLine();
       try
       {
           if (s == "Exit")
           {
                Environment.FailFast("Erreur fatale");
                return s;
           }
           else
           {
               return s;
           }
       }
       catch (Exception)
       {
           return "catch";
       }
       finally
       {
           s += "finally";
       }
   }
Run Code Online (Sandbox Code Playgroud)

据我所知,消息被写入,Windows application event log应用程序终止.

当我运行应用程序并将其Exit作为字符串放置时:

  1. debbugger表示错误:

在此输入图像描述

  1. 我试图找到事件日志文件,如msdn中所示,但我找不到它 在此输入图像描述

我不明白为什么应用程序没有抛出异常而没有关闭?对于第二点,我如何在PC中找到日志文件?

.net c# windows debugging exception

3
推荐指数
1
解决办法
1406
查看次数

将空项添加到有界组合框中

我需要在wpf mvvm应用程序中的有界组合框中添加一个空项目,我试过这个

<ComboBox TabIndex="23"  Text="{Binding Specialisation}" DisplayMemberPath="refsp_libelle">
      <ComboBox.ItemsSource>
                          <CompositeCollection >
                                        <ComboBoxItem  > </ComboBoxItem>
                                        <CollectionContainer  Collection="{Binding SpecialisationItemSource}" ></CollectionContainer>
                       </CompositeCollection>

     </ComboBox.ItemsSource>
  </ComboBox>
Run Code Online (Sandbox Code Playgroud)

它在我尝试添加空项目之前有效.

<ComboBox TabIndex="23" Text="{Binding Specialisation}" ItemsSource="{Binding SpecialisationItemSource}" DisplayMemberPath="refsp_libelle"/>
Run Code Online (Sandbox Code Playgroud)

所以我需要知道:

  1. 我犯的错误是什么?
  2. 我该如何解决?

谢谢,

.net c# wpf mvvm model-binding

3
推荐指数
1
解决办法
3185
查看次数

当记录存在时,无法将"System.DBNull"类型的对象强制转换为"System.Byte []"

我试图从MYSQL读取和下载BLOB.虽然记录存在,但我仍然继续得到这个错误.以下是我的代码:

this.Time = String.Format("{0:HH:mm:ss}", DropDownList1.SelectedValue);
            String query = "Select * from event where time='" + this.Time + "'";
            MySqlConnection conn = new MySqlConnection(connection);
            MySqlCommand cmd = new MySqlCommand(query, conn);
            String time = String.Format("{0:t}", DateTime.Today);
            conn.Open();
            MySqlDataReader r = cmd.ExecuteReader();   
            while (r.Read())
            {
                TextBox1.Text = r["name"].ToString();
                TextBox2.Text = r["Proposedby"].ToString();
                bytes = (byte[])r["proposalDoc"];    
                TextBox5.Text = Calendar1.SelectedDate.ToString("d");
                TextBox6.Text = r["time"].ToString();
                TextBox7.Text = r["Society"].ToString();
                TextBox8.Text = r["venue"].ToString();

            }
Run Code Online (Sandbox Code Playgroud)

请告诉我如何消除这个错误.

c# mysql asp.net ado.net webforms

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

在WPF应用程序中绑定可为空的日期时间

我有一个wpf应用程序,我将此属性绑定到一个datepicker

public Nullable<System.DateTime> dpc_date_engagement { get; set; }
Run Code Online (Sandbox Code Playgroud)

所以我添加了一个转换器:

 public class DateConverter : IValueConverter
   {
       public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value != null)
            return ((DateTime)value).ToShortDateString();
        return String.Empty;
    }

       public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
       {
           string strValue = value.ToString();
           DateTime resultDateTime;
           return DateTime.TryParse(strValue, out resultDateTime) ? resultDateTime : value;
       }
   }
Run Code Online (Sandbox Code Playgroud)

在XAML文件中:

                     <DatePicker >
                                <DatePicker.Text>
                                    <Binding Path="dpc_date_engagement" UpdateSourceTrigger="PropertyChanged">
                                        <Binding.Converter>
                                            <converter:DateConverter/>
                                        </Binding.Converter>
                                    </Binding>
                                </DatePicker.Text>
                            </DatePicker>
Run Code Online (Sandbox Code Playgroud)

问题是当日期为空时,显示的文本为1/1/0001.

  • 对于空值,如何修复我的代码以显示空字符串而不是01/01/0001?

.net c# wpf binding datetime

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

依赖注入和实体框架

我正在使用MVVM灯和它的Ioc做一个wpf应用程序SimpleIoc.

我实现了这样的存储库模式:

 public interface ICrud<T> where  T : class 
    {
        IEnumerable<T> GetAll();
        Task<IEnumerable<T>> AsyncGetAll(); 
        void AddNew(params T[] items);
        void Delete(params T[] items);
        void Update(params T[] items);
        void SaveOrUpdate(params T[] items);
    }

 public class Crud<T> : ICrud<T> where T : class 
    {

        public void AddNew(params T[] items)
        {
            using (var context = new DataEntities())
            {
                foreach (T item in items)
                {
                    context.Entry(item).State = System.Data.Entity.EntityState.Added;
                }
                context.SaveChanges();
            }
        }

        public void Delete(params T[] items)
        {
            using (var context = new …
Run Code Online (Sandbox Code Playgroud)

c# wpf entity-framework dependency-injection mvvm

2
推荐指数
1
解决办法
2072
查看次数

如何调用所有事件处理程序订阅者并获取其结果?

我有这个片段:

public static void Main()
{
    OnComparaison += LePlusPetit;
    OnComparaison += LePlusGrand;
    Console.WriteLine(OnComparaison(0, 9));
    Console.ReadKey();
}

public static int LePlusPetit(object obj1, object obj2)
{
    int int1 = (int)obj1;
    int int2 = (int)obj2;
    return (int1 < int2) ? int1 : int2;
}

public static int LePlusGrand(object obj1, object obj2)
{
    int int1 = (int)obj1;
    int int2 = (int)obj2;
    return (int1 > int2) ? int1 : int2;
}

public delegate int Comparer(object obj1, object obj2);
public static event Comparer OnComparaison;
Run Code Online (Sandbox Code Playgroud)

我一直都是这样的结果9 …

.net c# oop events behavior

2
推荐指数
1
解决办法
901
查看次数

通用类型与扩展方法

我需要在两种技术之间进行比较:使用泛型类型和扩展类型.我不是指一般的比较,我的意思是在这种特殊情况下我需要为一个名为类的类添加一些功能ClassA

  1. 使用泛型类型

    使用泛型类型(Where T: ClassA)并实现泛型方法

  2. 使用扩展方法

    使用ClassA添加其扩展方法

     public static class Helper
    {
    
     public static void MethodOne(this ClassA obj, )
    
     {
     //
      }
    
     }
    
    Run Code Online (Sandbox Code Playgroud)

我需要知道 :

  • 与其他技术相比,每种技术的优势是什么?
  • 为什么总是使用第一种技术Repository Pattern?例如,在此实现中,为什么我们不向全局类添加扩展方法Entity

.net c# oop generics extension-methods

2
推荐指数
1
解决办法
276
查看次数

Ajax调用web api方法

我有这个观点:

 <div id="addDisplay" class="display">
                <div class="box-header">
                    <h3 class="box-title">Ajouter un compte</h3>
                </div>
                @{
                    AjaxOptions addAjaxOpts = new AjaxOptions
                    {
                        // options will go here
                        OnSuccess = "getData", 
                        OnFailure="selectView('add')",
                        HttpMethod = "Post",
                        Url = "/api/AccountManage/CreateAccount"
                    };
                }
                @using (Ajax.BeginForm(addAjaxOpts))
                {

                @Html.Hidden("id", 0)
                <p>
                    <label>Statut social   </label>
                    <select id="marital_status" name="marital_status">
                        <option value="Mr">Monsieur</option>
                        <option value="MLLE">Mademoiselle</option>
                        <option value="MME">Madame</option>
                    </select>



                </p>
                <p><label>Nom   </label>@Html.Editor("Nom")<label style="color:red" id="labError"> ( * )</label></p>
                <p><label>Prenom   </label>@Html.Editor("Prenom")<label style="color:red" id="labError"> ( * )</label></p>
                <p><label>Adresse   </label>@Html.TextArea("address")<label style="color:red" id="labError"> ( * )</label></p>
                <p><label>Pseudo   </label>@Html.Editor("Username")<label style="color:red" id="labError"> …
Run Code Online (Sandbox Code Playgroud)

c# ajax asp.net-mvc jquery asp.net-web-api

2
推荐指数
1
解决办法
287
查看次数

使用Html帮助程序显示工具提示

我想在我的剃刀视图中添加一个工具提示,如下所示:

   @Html.Editor("Password", new { Title = "Doit contenir au moins une lettre majuscule, une minuscule, un chiffre et un caractère spécial" }) 
Run Code Online (Sandbox Code Playgroud)

它不起作用,如果我只是通过输入html标签更改html帮助器,它的工作原理!

我需要知道 :

  1. 这个错误的原因在哪里?
  2. 我该如何解决?

.net html asp.net asp.net-mvc razor

2
推荐指数
1
解决办法
8902
查看次数