开发者

GUI idea for WPF, though no clue how to implement it

开发者 https://www.devze.com 2023-04-10 07:13 出处:网络
What I\'ve got is basicly two classes Plugin and PluginLauncher Plugin is an abstract class that implements some functions to make a class a Plugin for my PluginLauncher class.

What I've got is basicly two classes Plugin and PluginLauncher

Plugin is an abstract class that implements some functions to make a class a Plugin for my PluginLauncher class.

PluginLauncher is a class that holds a collection (SortedDictionary) including some helper functions to start, stop, restart all or a specific Plugin.

It also loads all plugins on initialisation. Each plugin can be a .exe or .dll with a class the inherits from Plugin. An AppDomain is cre开发者_如何转开发ated for each plugin and communication is also setup for each Plugin (through a simple IPC messaging through sockets) (still has to be implemented)

I want to have a very, VERY basic GUI implementation that just has a list of all loaded Plugins, noting the plugin name, its state (which can be running, stopping, stopped, stoppedprematurely (an Enum)) and a button per plugin to start, stop or restart it.

I know I can add this functionality programmaticaly by just placing the elements on the GUI and calculating each X/Y location etc. but I am sure WPF has some pre-made 'functionalities' for this. But I am quite new to WPF and have no clue where to start looking.

A simple note: I am limited to .net 3.5 (or lower) thus no 4.0 elements.

I've included a very simple (hooray mspaint skills) example of what I had in mind.

GUI idea for WPF, though no clue how to implement it


The plugin nature of your app has little bearing on the mechanics of how you achieve this. Essentially, you need a collection of view models. Each item in that collection represents a plugin (but it could equally represent a customer or a chicken drumstick). You then bind an ItemsControl to this collection and define a template for how the item should be rendered.

Here is some pseudo-code to get you on your way:

public class PluginViewModel : ViewModel
{
    public string Name { get; }
    public PluginState State { get; private set; }
    public ICommand StartCommand { get; }
    public ICommand StopCommand { get; }
    public ICommand RestartCommand { get; }
}

public class PluginLauncherViewModel : ViewModel
{
    // use an ObservableCollection<PluginViewModel> to store your plugin view models
    public ICollection<PluginViewModel> Plugins { get; }
}

<ScrollViewer>
    <ItemsControl ItemsSource="{Binding Plugins}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                 <UniformGrid Rows="1">
                     <TextBlock Text="{Binding Name}"/>
                     <TextBlock Text="{Binding Status}"/>
                     <Button Command="{Binding StartCommand}">Start</Button>
                     <Button Command="{Binding StopCommand}">Stop</Button>
                     <Button Command="{Binding RestartCommand}">Restart</Button>
                 </UniformGrid>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</ScrollViewer>

Some problems you will no doubt bump into:

  • the DataContext of the outer XAML (ie. the ScrollViewer in my above example) must be an instance of PluginLauncherViewModel. How you wire this up is up to you, and there are varied options. Start with something simple like setting it in your code-behind.
  • ViewModel is a base class for all view models. See here for an example.
  • Your implementation of ICommand should be MVVM friendly. See here for an example.


For the simplest approach you might consider the Table element. For a more fine-grained control I'd recommend you using a Grid.

0

精彩评论

暂无评论...
验证码 换一张
取 消