小编Mar*_*tus的帖子

Caliburn ShowDialog和MessageBox

我正在使用caliburn为MVVM制作一个小型演示应用程序。

现在,我想展示一种MessageBoxMVVM方式。

对于对话框,我创建了一个事件,该事件在ShellView(根视图)中处理,并且仅WindowManager.ShowDialog使用Dialogs ViewModel类型进行调用。似乎对我来说坚持MVVM。

但是,显示消息框并获取其结果的方法是什么(好的还是取消的)?

我已经看到了这个问题,但是也没有答案。

艾森伯格先生亲自回答

“ Caliburn具有用于调用自定义消息框的内置服务。”

谁能说出他的意思?我没有在样本中看到它。

c# wpf messagebox mvvm caliburn.micro

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

用于性能测试的秒表

对于我Stopwatch用于性能测量的一些私人项目.

但是在我想测量的低重复数量的呼叫计数中,我最终得到0 ElapsedMilliseconds,这使得计算平均值变得困难.

我想过写自己的秒表课程.这可能与蜱计算,并给予一个模糊的ElapsedMicroseconds基础上Stopwatch.ElapsedTicksTimeSpan.TicksPerMillisecond.这可能不是一个很好的方法.

我肯定需要一些由winapi的高性能计数器支持的东西,所以datetime等等是不够的.

还有其他想法吗?

c# stopwatch performance-testing

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

网格和列表框有一些我无法摆脱的边框

我对WPF很新.我只是尝试使用Grid和Listbox进行一些布局,但是我有一些填充/间距/边距/边框(简称为边框),我无法逃脱.

边界围绕四个元素,元素本身没有问题,它们之间没有空间.

还尝试了WPF Inspector,但我无法找到它的来源.没有什么可看的.

这是我的XAML:

<Window x:Class="WpfElements.FourElements"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="FourElements" Height="701" Width="351">
<Grid Background="Red" Margin="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>

    <ListBox Margin="0" BorderThickness="0" Padding="0" Grid.Row="0" Grid.Column="0" Background="Lime" SelectionMode="Single" ItemsSource="{Binding Path=Texts}" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Disabled" VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch">
        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="0">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*" />
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>
                </Grid>
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBox Text="{Binding ItemText}" Grid.Row="{Binding Path=Row}" Grid.Column="{Binding Path=Col}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="0"></TextBox>
            </DataTemplate>
        </ListBox.ItemTemplate> …
Run Code Online (Sandbox Code Playgroud)

c# wpf xaml styles listbox

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

如何在企业项目中使用依赖注入

想象一下,你有一个应用程序,有数百个类实现了几十个"高级"接口(意味着组件级别).是否有推荐的依赖注射方式(如团结).是否应该有一个可以用于自举的"通用容器",可以作为Singleton访问?是否应该传递一个容器,所有实例都可以在RegisterInstance中传递?应该在启动时的某个地方通过RegisterType完成所有事情吗?如何在需要时使容器可以访问.构造函数注入似乎是错误的,作为标准方式的争论,您必须将接口从组件级别传递到在启动时使用它的非常低的位置,或者引用最终在"知道您居住的地方"反模式.而且:拥有一个"可用"的容器可能会让开发人员想到在客户端上下文中解析服务器组件.怎么避免呢?

欢迎任何讨论!


编辑澄清

我想出了一个有点真实世界的例子,可以更好地了解我看到的问题.

让我们想象一下应用程序是一个高保真系统.该系统具有CD播放器(集成在CD机架中)和USB端口(集成在USB机架中)以播放音乐.

现在,CD播放器和USB端口应能播放mp3音乐.

我周围有一个mp3解码器,可以注射.

现在我开始使用高保真系统.还没有插入CD,也没有插入usb棒.我现在不需要mp3解码器.

但是使用构造函数注入,我必须已经将依赖注入cd机架和USB机架.

如果我从不插入MP3 CD或MP3 usb棒怎么办?

我应该在CD机架中保留一个参考,所以当插入一张mp3光盘时,我手头有一个装饰器吗?(对我来说似乎不对)

cd机架的子系统需要解码器,只有在插入mp3时才会启动解码器.现在我在CD机架上没有容器,这里的构造函数注入怎么样?

c# architecture dependency-injection inversion-of-control

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

使用Caliburn Micro进行导航

我正在玩Caliburn.Micro并且现在有一个非常简单的应用程序.

它有一个AppView,它实际上有一个用于NavigationBar的ContentControl,一个InnerView和一个StatusBar.

现在我想处理不同内部视图之间的导航.

现在我使用eventaggregator发布一个NavigationEvent,它应该将主窗口的内部视图切换到另一个视图.

这是我对Publish的调用(所有InnerView都有相同的基类,它有一个IEventAggregator)

public void NavigateOverview()
{
    base._eventAggregator.Publish(new NavigateEvent("OverviewViewModel"));
}
Run Code Online (Sandbox Code Playgroud)

现在我将一个字符串传递给AppViewModel,它处理NavigateEvent:

        public void Handle(NavigateEvent navigate)
        {
            InnerViewModel target;

            switch (navigate.TargetViewModel)
            {
                case "SelectProjectViewModel":
                {
                    target = new SelectProjectViewModel(_eventAggregator);
                    break;
                }
                case "OverviewViewModel":
                {
                    target = new OverviewViewModel(_eventAggregator);
                    break;
                }
                default:
                {
                    throw new InvalidOperationException("no target type found");
                }
            }

            this.CurrentInnerViewModel = target;
        }
Run Code Online (Sandbox Code Playgroud)

通过字符串工作,但是错误并且不是很干净.

什么是Caliburn处理方式?这是导体应该做的吗?

c# mvvm caliburn.micro

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

子项的 TreeView ItemContainerStyle、FullRowSelect

在这里使用了为 TreeViewItems 设置 fullrowselect 的方法:Lee Campbell Horizo​​ntal Stretch for TreeViewItems

这对于顶级项目非常有效。

但是集合好像有问题TreeViewItem.Items ,好像没有采取正确的模板。(我发现它不在TreeView.Items集合中,正如模板所示)

有没有一种简单的方法可以从父项样式派生子项样式?

如何描述一次样式(对于所有 TreeviewItems)并让该样式用于所有 TreeViewItems,无论它们是在集合中TreeView.Items还是在TreeViewItem.Items集合中?

这是我的 XAML,一些代码来自 LeeCampbell 示例。

<Window x:Class="TreeViewFullRow.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:TreeViewFullRow"
        Title="MainWindow"
        Width="525"
        Height="350">

    <Window.Resources />

    <Grid>

        <TreeView Name="TreeData"
                  Width="236"
                  Height="311"
                  HorizontalAlignment="Left"
                  VerticalAlignment="Top">

            <TreeView.ItemContainerStyle>




                <Style BasedOn="{StaticResource {x:Type TreeViewItem}}" TargetType="TreeViewItem">
                    <Setter Property="HorizontalContentAlignment" Value="Center" />
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="TreeViewItem">
                                <StackPanel>
                                    <Grid>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="Auto" MinWidth="19" />
                                            <ColumnDefinition Width="*" />
                                        </Grid.ColumnDefinitions>
                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="Auto" />
                                            <RowDefinition />
                                        </Grid.RowDefinitions>
                                        <!-- …
Run Code Online (Sandbox Code Playgroud)

c# wpf xaml

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

IList in interface,List in implementation

在我的接口中,通常有一个IList<ISomeType>代表List类型的成员,并且说我期望一个支持Add方法的实现.

但是在接口实现中,有一个

IList<ISomeType> = new List<ISomeType>()
Run Code Online (Sandbox Code Playgroud)

例如,每当我使用List时,我都必须进行转换

(this.MyList as List<IMyType>).AddRange(someType.ToList());
Run Code Online (Sandbox Code Playgroud)

有一个更好的方法吗?如何避免这种演员?

- 编辑以询问更多信息 -

而不是扩展方法,是否有一个小的linq表达式来解决这个问题?

IList<string> list = new List<string>();
var items = new[] { "1", "2", "3" };
items.ToList().ForEach(x => list.Add(x));
Run Code Online (Sandbox Code Playgroud)

但这看起来并不是很直接,因为它颠覆了正在做的事情.(操作在要添加的项目上,而不在列表中).

有什么比这更好的?可以在列表上做些什么?

c# abstraction interface list

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

在空闲端口上进行 UDP 发送/接收

在一个项目中,有一个设备侦听特定的 UDP 端口并应答发送方端口。

对于发送方和接收方,我希望系统选择一个空闲端口,所以我有以下代码:

[请原谅大量代码,但这是显示发生的行为的最小示例]

发送代码:

public class UdpSender
{
    public int Port = 0; // some initially random port
    public UdpClient UdpServer { get; set; }

    public UdpSender()
    {
        UdpServer = CreateUdpClient();

        // save the portnumber chosen by system to reuse it
        Port = ((IPEndPoint)(UdpServer.Client.LocalEndPoint)).Port;
    }

    private UdpClient CreateUdpClient()
    {
        if (UdpServer != null)
        {
            UdpServer.Close();
        }

        IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, Port);

        var udpServer = new UdpClient();
        udpServer.ExclusiveAddressUse = false;
        udpServer.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
        udpServer.Client.Bind(localEndPoint);

        return udpServer;
    }

    public …
Run Code Online (Sandbox Code Playgroud)

c# sockets port networking udp

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

IIS中托管的WCF服务库的log4net

对于一个项目,我有一个WCF服务库(目前非常简单),它通过WCF服务网站项目在IIS 7.5中托管.

对于该WCF服务库,我需要log4net来记录一些重大事件.

但是在启动和访问网站后,没有创建日志文件.

以下是我的配置详细信息:

WCF服务库:

App.config (is containing the following)

<appSettings>
    <add key="log4net-config-file" value="log4net.config"/>
    <!-- <add key="log4net.Internal.Debug" value="true"/> -->
</appSettings>

log4net.config

<?xml version="1.0"?>
<configuration>
  <configSections>
    <section name="log4net" type="System.Configuration.IgnoreSectionHandler"/>
  </configSections>
  <log4net>
    <root>
      <level value="DEBUG"/>
      <appender-ref ref="RollingLogFileAppender"/>
    </root>
    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="C:\\Temp\\Logfile.txt"/>
      <appendToFile value="true"/>
      <rollingStyle value="Size"/>
      <datePattern value="yyyyMMdd"/>
      <maxSizeRollBackups value="5"/>
      <maximumFileSize value="100KB"/>
      <staticLogFileName value="true"/>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%d [%t] %-5p %c - %m%n"/>
      </layout>
    </appender>
  </log4net>
</configuration>

In AssemblyInfo.cs
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]

On top of the …
Run Code Online (Sandbox Code Playgroud)

c# iis wcf log4net

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

Distinct()如何在对象列表中查找唯一元素

有一个非常简单的类:

public class LinkInformation
{
    public LinkInformation(string link, string text, string group)
    {
        this.Link = link;
        this.Text = text;
        this.Group = group;
    }

    public string Link { get; set; }
    public string Text { get; set; }
    public string Group { get; set; }

    public override string ToString()
    {
        return Link.PadRight(70) + Text.PadRight(40) + Group;
    }
}
Run Code Online (Sandbox Code Playgroud)

我创建了这个类的对象列表,包含多个重复项.

所以,我尝试使用Distinct()获取唯一值的列表.

但它不起作用,所以我实施了

IComparable<LinkInformation>

    int IComparable<LinkInformation>.CompareTo(LinkInformation other)
    {
        return this.ToString().CompareTo(other.ToString());
    }
Run Code Online (Sandbox Code Playgroud)

然后...

IEqualityComparer<LinkInformation>

    public bool Equals(LinkInformation x, LinkInformation y)
    {
        return …
Run Code Online (Sandbox Code Playgroud)

c# linq distinct

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

Visual Studio 2010:添加现有文件夹

我想使用很多已经创建并存储在文件夹中的东西.在一个新项目中,我想使用这些类,只需添加整个文件夹,包括文件夹本身,以保持项目的清晰排列.

如何将现有文件夹添加到项目中?

由于该文件夹位于并发版本系统上,我不想复制/移动/创建文件,尤其是文件夹.

projects-and-solutions visual-studio-2010

0
推荐指数
1
解决办法
2310
查看次数

为什么没有空的字面值?

是否有任何特定原因没有空char文字?

什么最接近我的想法,'''\0'空字符.

C++char由表示的int,这意味着空炭直接到0的整数值,这是在C++"一样空".

提出这个问题的实际部分:

在类中,我想将char值表示为枚举属性.没有偏见我试图初始化一个实例'',当然这不起作用.但是不应该char有空值吗?不要混淆string.Empty,更多的是在性质上null reference.

所以问题是:为什么没有空的char?

-编辑-

看到这个问题,可以增强以下问题:空char值可以在不破坏字符串的情况下连接字符串和字符.这不是更好吗?或者这应该"按预期工作"?

c# language-design char

0
推荐指数
1
解决办法
6646
查看次数

带有单独配置的IIS WCF服务的log4net

我想将log4net用于IIS中托管的WCF服务.

但是要轻松更改设置,我想使用单独的配置文件.

所以我添加到Web.Config(以及WCF服务库的App.config)

<appSettings>
    <add key="log4net_config" value="log4net.config" />
</appSettings>
Run Code Online (Sandbox Code Playgroud)

但是这会导致IIS的当前目录

C:\Windows\System32\inetsrv
Run Code Online (Sandbox Code Playgroud)

并且永远不会有我的log4net.config文件.

但我想配置log4net之类的东西

var configFile = ConfigurationManager.AppSettings["log4net_config"];
var fileInfo = new FileInfo(configFile);
XmlConfigurator.ConfigureAndWatch(fileInfo);
Run Code Online (Sandbox Code Playgroud)

如何配置满足此需求的目录?

c# asp.net iis wcf log4net

0
推荐指数
1
解决办法
373
查看次数