小编Kry*_*oxx的帖子

访问MVVM Light中其他ViewModel中的属性

我有一个主要ViewModel包含我正在使用的一定数量的项目列表UserControls,它们显示在ContentControl主视图中.我现在的之间交换数据的方式ViewModels存在,使得每个的一个参考的ViewModels主ViewModel,主要的一个ViewModel在每一个UserControl.但是我不知道这是不是这样做的正确方法,因为我有一个ViewModelLocator,我希望这个类有一些功能来支持这样的事情.

任何人都可以告诉我,如果我正在做这个,或者在MVVM Light中有更好的方法吗?

编辑:

当我在寻找不同的东西时,我发现了这个,这会是一个更好的解决方案吗?

private ViewModelLocator locator = new ViewModelLocator();
Run Code Online (Sandbox Code Playgroud)

然后使用定位器属性来引用每个ViewModel?

EDIT2:

显然我认为可以工作的不是,起初我只有主要的参考ViewModel,这工作,但当我尝试这种UserControls方式崩溃我的应用程序.我正在尝试第一次编辑的可能解决方案.

MainViewModel.cs(其他类似,仅参考主ViewModel)

public class MainViewModel : ViewModelBase
{
    private ItemAddViewModel itemAddViewModel;
    private ItemsViewModel itemsViewModel;

    /// <summary>
    /// Initializes a new instance of the MainViewModel class.
    /// </summary>
    public MainViewModel()
    {
        ItemsList = Item.GetItemsList();

        itemAddViewModel = ServiceLocator.Current.GetInstance<ItemAddViewModel>();
        itemsViewModel = ServiceLocator.Current.GetInstance<ItemsViewModel>();

        ShowItemsView();
    }
...
    private void ShowItemsView() …
Run Code Online (Sandbox Code Playgroud)

c# mvvm-light

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

使用ServletContextListener和SLF4J在contextDestroyed上没有日志输出

我正在尝试向记录器写一条消息(Vaadin)servlet已停止,这使用SLF4J和Log4j2.

为此,我使用的是ServletContextListener在应用程序启动时记录消息.但是在登录contextDestroyed方法时我无法获得任何输出...这是我的实现:

@WebListener
public class VaadinLogger implements ServletContextListener {

    private static final Logger logger = LoggerFactory.getLogger(VaadinLogger.class);

    @Override
    public void contextInitialized(ServletContextEvent contextEvent) {
        // Remove appenders from JUL loggers
        SLF4JBridgeHandler.removeHandlersForRootLogger();

        // Install bridge
        SLF4JBridgeHandler.install();

        // Get servlet context
        ServletContext context = contextEvent.getServletContext();

        // Retrieve name
        String name = context.getServletContextName();

        // Log servlet init information
        logger.info("Start \"{}\"", name);
    }

    @Override
    public void contextDestroyed(ServletContextEvent contextEvent) {
        // Get servlet context
        ServletContext context = contextEvent.getServletContext();

        // Retrieve name
        String name …
Run Code Online (Sandbox Code Playgroud)

java logging servlets slf4j log4j2

7
推荐指数
1
解决办法
1140
查看次数

SizeToContent绘制不需要的边框

每当我尝试制作一个窗口并将其设置SizeToContent为时WidthAndHeight,在打开窗口时正确调整其内容的大小,但它会在右侧和底部添加一个小边框.在调整大小时,它会消失,当使用设置的高度和宽度时,也不会出现此问题.

这是我的意思的样本:

在此输入图像描述

你可以说这不是一个大问题,虽然我发现它使我的应用程序看起来不专业,特别是当我需要提出这个时.有谁知道为什么会这样,或者是否有解决方法?我正在用C#编写这个项目.

XAML代码:

<Window x:Class="FPricing.InputDialog"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="InputDialog" Width="400" Height="300" SizeToContent="WidthAndHeight">
    <StackPanel>
        <Label x:Name="question">?</Label>
        <TextBox x:Name="response"></TextBox>
        <Button Content="OK" IsDefault="True" Click="Button_Click" />
    </StackPanel>
</Window>
Run Code Online (Sandbox Code Playgroud)

在创建类时传递值.

但是,即使没有自定义底层代码,我也会在我创建的每个窗口上遇到此问题.

c# wpf sizetocontent

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

当BufferBlock为空时,Async StreamWriter循环停止写入

我最近将我的LogManager类从一个转换static为singleton类,以便在WPF中更容易绑定(虽然我认为此问题在此之前也存在).检查每一件事是否一切正常后,我注意到一些奇怪的事 在SaveLoop我用我写日志文件,并将它们发送到视图的任务,因为我觉得它确实不太正常工作.

在视图中,它们几乎立即被添加,如果我关闭应用程序,SaveLoop任务将被取消,我可以在创建的文件中查看日志.但是,当应用程序崩溃时(或者我在Visual Studio中按停止按钮),带有日志的文件比视图中的实际日志数少约8-10个日志.

我注意到当写入不再BufferBlock包含日志时,编写器停止写入(在消息中间),并且只有在添加新日志时才会继续写入.

是因为BufferBlock停止任务中的每个异步操作,因为它没有更多的项目要提供?

这是我运行应用程序时控制台中的输出(LogStatus消息是另一个检测到过去1000毫秒内发生了任何变化的循环任务):

12345678
12345678
12345678
1Setting LogStatus=Idle
Run Code Online (Sandbox Code Playgroud)

我真的不知道如何处理这个问题,如果有人可以帮助我,将不胜感激!


SaveLoop任务(控制台输出仅用于调试目的):

private async Task SaveLoop(CancellationToken cancel)
{
    string logPath = "logs\\";
    string logFile = _startTime.ToString("yyyy-MM-dd_HH-mm-ss") + ".log";
    string fullPath = Path.Combine(logPath, logFile);

    if (!Directory.Exists(logPath))
        Directory.CreateDirectory(logPath);

    using (FileStream fileStream = new FileStream(fullPath, FileMode.Append, FileAccess.Write, FileShare.Read, 8192, useAsync: true))
    {
        using (StreamWriter writer = new StreamWriter(fileStream))
        {
            while (true)
            {
                LogMessage logMessage = null;

                try …
Run Code Online (Sandbox Code Playgroud)

c# asynchronous stream streamwriter task-parallel-library

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

SteamWriter不想创建文件

我正在尝试将我的日志写入文件,因此每当我的应用程序崩溃时,日志仍保存在日志文件中.然而,StreamWriter不允许我,我已经尝试了每个路径的可能性,但每次我尝试测试它时它会抛出这个异常:

System.NotSupportedException: The given path's format is not supported.
Run Code Online (Sandbox Code Playgroud)

然而路径应该没问题,如控制台输出中所示:

StartLogging=True Trying to write to C:\Users\Tom\documents\visual studio 2012\Projects\xxx\yyy\bin\Debug\logs\06-11-13_11:51:37.log
Run Code Online (Sandbox Code Playgroud)

任何帮助将不胜感激,因为我不明白为什么这不起作用...

SaveLog功能:

    public void SaveLog(bool startLogging = false)
    {
        DateTime startTime = LogManager.StartTime;
        string appPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
        string logPath = appPath.Replace("file:\\", "") + "\\logs\\";
        string logFile = startTime.ToString("MM-dd-yy_HH:mm:ss") + ".log";
        string fullPath = Path.Combine(logPath, logFile);

        if (!Directory.Exists(logPath))
        {
            Directory.CreateDirectory(logPath);
        }

        Console.WriteLine("StartLogging={0} Trying to write to {1}", startLogging, fullPath);
        using (StreamWriter writer = new StreamWriter(fullPath, !startLogging))
        {
            writer.Write("[{0}][{1}] ", Time.ToString("HH:mm:ss:fff"), Level.ToString());
            writer.Write(Message);
            writer.WriteLine(" …
Run Code Online (Sandbox Code Playgroud)

c#

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

不要在Entity Framework中映射ReactiveUI属性

使用实体框架代码首先,我创建了一些对象来存储数据库中的数据.我在这些对象中的ReactiveUI库中实现了ReactiveObject类,因此每当prorerty更改为响应更快的UI时,我都会收到通知.

但实现它会为我的对象添加3个属性,即Changed,Changing和ThrowExceptions.我真的不认为这是一个问题,但是当在DataGrid中加载表时,这些都会得到一个列.

有没有办法隐藏这些属性?我不能只手动定义列,因为我的所有表都有1个数据网格,我从组合框中选择.

下面找到的解决方案也在这里:当AutoGenerateColumns = True时,有没有办法隐藏DataGrid中的特定列?

    void dataTable_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
    {
        List<string> removeColumns = new List<string>()
        {
            "Changing",
            "Changed",
            "ThrownExceptions"
        };

        if (removeColumns.Contains(e.Column.Header.ToString()))
        {
            e.Cancel = true;
        }
    }
Run Code Online (Sandbox Code Playgroud)

c# entity-framework reactiveui

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