I have a wpf MultiROIStats.dll with mode, view, ViewModel. Here is the C# of the view:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace MultiROIStats
{
using ViewModel;
//xmlns:dg="clr-namespace:Microsoft.Windows.Controls;assembly=WpfToolkit"
/// <summary>
/// Interaction logic for MultiROIStats.xaml
/// </summary>
public partial class MultiROIStats : Window
{
public MultiROIStats()
{
InitializeComponent();
DataContext = new MultiROIStatsViewModel();
}
}
}
To use this MultiROIStats,dll, I insert it info the reference of another project. Now I need to access the ViewModel (some methods there) of the inserted MultiROIStats.dll. I am wondering how should I do this? I initiated an object of the inserted MultiROIStats.dll, but cannot find the method I want to use in its ViewModel:
private void btnSave_Click(object sender, RoutedEventArgs e)
{
MultiROIStats mroi = new MultiROIStats();
mroi.Show();
// here should be mroi.viewmode.dothings() ... but I don't know how to access it
}
You should be able to get to it by casting the DataContext to the view-model type:
MultiROIStats mroi = new MultiROIStats();
mroi.Show();
var viewmodel = mroi.DataContext as MultiROIStatsViewModel;
if (viewmodel != null)
viewmodel.dothings();
var window = new MultiROIStats();
window.Show();
var vm = window.DataConntext as MultiROIStatsViewModel;
vm.DoThings();
Related
So I'm making a WPF application where I'm using CSharpCodeProvider to compile code stored in string. Everything works great except for one part. When I try to use Process.Start(), it gives me "Metadata file 'System.Diagnostics.dll' could not be found" error. I don't know what that means.
using Microsoft.CSharp;
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace Dynamically_compile_codes
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
CSharpCodeProvider codeProvider = new CSharpCodeProvider(new Dictionary<string, string>() { { "CompilerVersion", txtFrameWork.Text } });
Button button = (Button)sender;
CompilerParameters parameters = new CompilerParameters(new[] { "mscorlib.dll","System.Core.dll","System.Diagnostics.dll"}, txtOutput.Text,true);
//generate exe, not dll
parameters.GenerateExecutable = true;
CompilerResults results = codeProvider.CompileAssemblyFromSource(parameters, textBox.Text);
if (results.Errors.HasErrors)
{
results.Errors.Cast<CompilerError>().ToList().ForEach(error=> txtStatus.Text+=error.ErrorText+"/r/n");
}
else
{
//If we clicked run then launch our EXE
Process.Start(txtOutput.Text);
}
}
}
}
And here is the code that is stored in the string:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
namespace Hidden_Console_Application
{
class Program
{
static void Main(string[] args)
{
Process.Start("explorer.exe");
}
}
}
System.Diagnostics is a namespace that resides in the System.dll assembly. The CompilerParameters constructor expects a string collection of assembly names and is therefore looking for a loaded assembly named System.Diagnostics which does not exist.
Should be:
CompilerParameters parameters = new CompilerParameters(new[] { "mscorlib.dll","System.Core.dll","System.dll"}, txtOutput.Text,true);
I am trying to jump to an error page if reading data fails, although I thought only if usercontrol activates(eg. button pressed) can successfully navigate, but is there anyway to do it automactally?
Window
|--Page
|--Page
Windows
--Page
--Page
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml;
namespace Launcher
{
/// <summary>
/// Interaction logic for BootPage.xaml
/// </summary>
public partial class BootPage : Page
{
public BootPage()
{
InitializeComponent();
readConfig();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
bootError(1001); **//This works**
}
void readConfig()
{
string gamePath;
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("e:\\bootstrap.xml");
XmlNodeList boot = xmlDoc.DocumentElement.SelectNodes("/config/startup/arkkdmca/boot");
foreach (XmlNode node in boot)
{
try
{
gamePath = node.SelectSingleNode("drive").InnerText + ":\\" + node.SelectSingleNode("directory").InnerText;
MessageBox.Show(gamePath);
}
catch(Exception)
{
gamePath = "null";
bootError(1001); **//This Failed**
return;
}
}
}
void bootError(int errorCode)
{
this.NavigationService.Navigate(new ErrorPage(errorCode));
}
}
}
So basically.. I added 2 new pages in a frame and when I press a button it changes frame..
But if I check a checkbox on the first page it wont be checked if I go to another page and then go back to the first one..
here is a more visual look http://recordit.co/Py3zptKLck
Source code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace CheckboxesAndPages
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void button_Click(object sender, RoutedEventArgs e)
{
main.Content = new SecondPage();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
main.Content = new thirdPage();
}
}
}
There is no source code for the 2 pages.
You are giving it a new SecondPage every time
SecondPage secondPage = new SecondPage();
private void button_Click(object sender, RoutedEventArgs e)
{
main.Content = secondPage;
}
I tried dragging a data source object from my Patients table into the form by expanding patients, going to appointments (it is there because it is connected with a foreign key) and dragging the appointment time onto the form.
When I run the program, I get "Cannot bind to the property or column Ap_time on the DataSource."
I am at a loss and am stressing out over this.
Heres my code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Validation;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class PatientMaster : Form
{
public PatientMaster()
{
InitializeComponent();
}
ClassLibrary3.HospitalEntities dbcontext = new ClassLibrary3.HospitalEntities();
private void PatientMaster_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
dbcontext.Patients.OrderBy(patient => patient.Pat_Last_Name).ThenBy(patient => patient.Pat_First_Name).Load();
patientBindingSource.DataSource = dbcontext.Patients.Local;
}
}
}
I think you are missing ToBindingList():
patientBindingSource.DataSource = dbcontext.Patients.Local.ToBindingList();
Hi I am trying to programmatically control a WPF animation but am getting the error above, could someone help with the error - not very familiar with c# - thanks
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Animation;
namespace WpfApplication10
{
///
/// Interaction logic for Window1.xaml
///
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
AnimationClock clock;
void StartAnimation()
{
DoubleAnimation animate = new DoubleAnimation();
animate.To = 300;
animate.Duration = new Duration(TimeSpan.FromSeconds(5));
animate.RepeatBehavior = RepeatBehavior.Forever;
clock = animate.CreateClock();
test.ApplyAnimationClock(Ellipse.WidthProperty, clock);
}
void PauseAnimation()
{
clock = new AnimationClock();
clock.Controller.Pause();
}
void ResumeAnimation()
{
clock.Controller.Resume();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
PauseAnimation();
}
}
}
It means you cannot create an instance of the "clock" object using "new". You can do it using the animation.CreateClock() method like the one in your StartAnimation() method. Anyway, a little tweak on your code should make it work. Hope the code below gives you an idea:
using System;
using System.Windows.Media.Animation;
using System.Windows;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Shapes;
namespace WpfApplication10 { /// /// Interaction logic for Window1.xaml ///
public partial class Window1: Window
{
public Window1()
{
InitializeComponent();
DoubleAnimation animate = new DoubleAnimation();
animate.To = 300;
animate.Duration = new Duration(TimeSpan.FromSeconds(5));
animate.RepeatBehavior = RepeatBehavior.Forever;
clock = animate.CreateClock();
}
AnimationClock clock;
void StartAnimation()
{
test.ApplyAnimationClock(Ellipse.WidthProperty, clock);
}
void PauseAnimation()
{
clock.Controller.Pause();
}
void ResumeAnimation()
{
clock.Controller.Resume();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
StartAnimation();
}
}
}