我正在创建一个MVVM应用程序.
在我的模型中,我需要处理在View中显示的System.Windows.Forms.Panel.我的想法是在ViewModel中创建这个Panel,然后从一侧 - 将View绑定到它,另一边将其句柄传递给模型.
我有一个WindowsFormsHost控件:
<Page x:Class="Test.Views.RenderPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:WinForms="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Test.Views"
mc:Ignorable="d"
d:DesignHeight="800" d:DesignWidth="1200"
Title="Page1">
<DockPanel>
<WindowsFormsHost x:Name="winformsHost" Child="{Binding RenderPanel}"/>
</DockPanel>
</Page>
Run Code Online (Sandbox Code Playgroud)
我想将它的Child属性与ViewModel提供的RenderPanel绑定
public ObservableObject<System.Windows.Forms.Panel> RenderPanel { get; private set; }
public VideoRecorderViewModel ()
{
RenderPanel = new System.Windows.Forms.Panel (); //Bind it here
var model = new Model (RenderPanel.Handle); pass it to the model
}
Run Code Online (Sandbox Code Playgroud)
但是,我收到一个错误说:
System.Windows.Markup.XamlParseException:
A 'Binding' cannot be set …Run Code Online (Sandbox Code Playgroud) 我想阅读Windows的事件日志.我不确定这是不是最好的方法,但我想使用pywin32 - > win32evtlog模块来做到这一点.首先,可以使用此库从Windows 7读取日志,如果是这样,如何读取与应用程序运行相关的事件(运行.exe必须在Windows中的事件日志中留下痕迹我猜).
我已经设法在网上找到一些小例子,但这对我来说还不够,遗憾的是文档写得不好; /
import win32evtlog
hand = win32evtlog.OpenEventLog(None,"Microsoft-Windows-TaskScheduler/Operational")
print win32evtlog.GetNumberOfEventLogRecords(hand)
Run Code Online (Sandbox Code Playgroud) 我想在我的 PyQt4 GUI 中创建一个导航工具栏,但我无法让它工作,我必须承认我主要是复制粘贴了这段代码,但我并没有真正理解它。如果您能告诉我如何添加 NavigationToolbar 并解释它是如何制作的,那就太好了。先感谢您。
顺便提一句。我看过这篇文章,但我相信它与我的不同,我不知道如何使用它。
from PyQt4 import QtGui
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
class MplCanvas(FigureCanvas):
def __init__( self ):
self.fig = Figure()
self.ax = self.fig.add_subplot( 111 )
FigureCanvas.__init__( self, self.fig )
FigureCanvas.setSizePolicy( self, QtGui.QSizePolicy.Expanding,QtGui.QSizePolicy.Expanding )
FigureCanvas.updateGeometry( self )
class matplotlibWidget(QtGui.QWidget):
def __init__( self, parent = None ):
QtGui.QWidget.__init__( self, parent )
self.canvas = MplCanvas()
#self.toolbar = self.canvas.toolbar #Dunno How :(
self.vbl = QtGui.QVBoxLayout()
self.vbl.addWidget( self.canvas )
self.setLayout( self.vbl )
Run Code Online (Sandbox Code Playgroud) 我有一个结构列表:
struct MyStruct{
int ID;
string name;
string surname;
}
List<MyStruct> list1 = new List<MyStruct>();
///here the list is being updated so that it has some elements
Run Code Online (Sandbox Code Playgroud)
现在我想从list1中提取所有ID并放入另一个列表:
List<int> list2 = new List<int>();
list2.AddRange(list1.GetIds() /*LINQ HERE*/);
Run Code Online (Sandbox Code Playgroud)
诀窍是我想用LINQ来做,但我仍然不太了解它.我知道它可以用普通的for循环完成,但我想使用AddRange和LINQ.
我有一个抽象类,我想为从我的抽象类继承的所有类实现Singleton模式.我知道我的代码不起作用,因为会有元类属性冲突.任何想法如何解决这个问题?
from abc import ABCMeta, abstractmethod, abstractproperty
class Singleton(type):
_instances = {}
def __call__(cls, *args, **kwargs):
if cls not in cls._instances:
cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
return cls._instances[cls]
class GenericLogger(object):
__metaclass__ = ABCMeta
@abstractproperty
def SearchLink(self): pass
class Logger(GenericLogger):
__metaclass__ = Singleton
@property
def SearchLink(self): return ''
a = Logger()
Run Code Online (Sandbox Code Playgroud) 我想结合QtDesigner生成的Ui.我想将它与myplotlib合并,以便所有内容都在一个窗口中.当我运行以下代码时,我收到一个错误:
"QWidget::setLayout: Attempting to set QLayout "" on Ui_MainWindow "", which already has a layout
Run Code Online (Sandbox Code Playgroud)
我知道我应该将我的画布布局与某些内容联系起来,但我不知道是什么.我希望你能在这里帮助我:
from PyQt4 import QtCore, QtGui
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QT as NavigationToolbar
import matplotlib.pyplot as plt
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_MainWindow(QtGui.QMainWindow):
def __init__(self, parent = None):
super(Ui_MainWindow, self).__init__(parent)
# …Run Code Online (Sandbox Code Playgroud) 我有这个代码将在Distinct()将返回的每个对象上执行Console.WriteLine(x).
objects.Distinct().ToList().ForEach(x => Console.WriteLine(x));
Run Code Online (Sandbox Code Playgroud)
但是,如何在不使用ToList()的情况下实现相同的功能?
我正在使用Prism Framework编写MVVM应用程序.当属性值更改时,我无法更新标签.当我创建模型并为属性分配初始值时,绑定到它的标签会更新.但是当我在应用程序生命周期内更改属性时,标签将不会更新其内容.
这是我的xaml:
<Window x:Class="Project.Views.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="700" Width="700">
<DockPanel LastChildFill="True">
<Button x:Name="btnStart" Command="{Binding Path=Start}" Content="StartProcess"/>
<GroupBox Header="Current Operation">
<Label x:Name="lblCurrentOperation" Content="{ Binding CurrentOperationLabel, UpdateSourceTrigger=PropertyChanged}"/>
</GroupBox>
</DockPanel>
</Window>
Run Code Online (Sandbox Code Playgroud)
这是我的ViewModel:
public class MyModelViewModel : BindableBase
{
private MyModel model;
private string currentOpeartion;
public DelegateCommand Start { get; private set; }
public string CurrentOperationLabel
{
get { return currentOpeartion; }
set { SetProperty(ref currentOpeartion, value); }
}
public MyModelViewModel ()
{
model = new MyModel ();
Start = new DelegateCommand (model.Start); …Run Code Online (Sandbox Code Playgroud)