System.Windows.Media.Animation.AnimationTimeline)' is inaccessible due to its protection level - c#

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();
}
}
}

Related

csharpcodeprovider: Can't use Process.Start()

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 thought only usercontrol can navigate page inside a window frame in WPF app, is there any workarounds?

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));
}
}
}

Why are my checkboxes unchecking when changing Page? WPF C# .NET

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;
}

Access ViewModel of a wpf dll

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();

Datagrid add items to datatable from another window

I got a little problem in my program here. I am trying to add items to my datatable and then get it to show on my datagrid. I can do it with a simple code from my main page but I want an external page to send the results to me but I cant find out what I am doing wrong here.
MainWindow
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.Data;
namespace Træner_Program
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Display();
}
public DataTable dt = new DataTable();
public DataTable dtBind = new DataTable();
public void Display()
{
//I create the data in memory
//usually the date is get from DataBase and cached in memory to enhance the performance
dt = new DataTable();
dt.Columns.Add("Øvelse");
dt.Columns.Add("Vægt");
dt.Columns.Add("Sæt");
dt.Columns.Add("Gentagelser");
dt.Columns.Add("Pause");
dtBind = dt.Copy();
Binding bind = new Binding();
bind.Source = dtBind;
this.dtgPlan.SetBinding(ListView.ItemsSourceProperty, bind);
}
public void AddRow(string Øvelse, string Vægt, string Sæt, string Gentagelser, string Pause)
{
DataRow dr = dt.NewRow();
dr["Øvelse"] = Øvelse;
dr["Vægt"] = Vægt;
dr["Sæt"] = Sæt;
dr["Gentagelser"] = Gentagelser;
dr["Pause"] = Pause;
dt.Rows.Add(dr);
dtBind = dt.Copy();
Binding bind = new Binding();
bind.Source = dtBind;
this.dtgPlan.SetBinding(ListView.ItemsSourceProperty, bind);
}
private void btnGem_Click(object sender, RoutedEventArgs e)
{
//this is the working command from mainpage
//AddRow("Bænkpres", "80", "3", "10", "50");
}
private void btnTilføj_Click(object sender, RoutedEventArgs e)
{
var tilføjØvelse = new Tilføj_Øvelse();
tilføjØvelse.Show();
}
}
}
Add items
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.Shapes;
namespace Træner_Program
{
/// <summary>
/// Interaction logic for Tilføj_Øvelse.xaml
/// </summary>
public partial class Tilføj_Øvelse : Window
{
MainWindow mw = new MainWindow();
public Tilføj_Øvelse()
{
InitializeComponent();
}
private void btnTilføj_Click(object sender, RoutedEventArgs e)
{
mw.AddRow("Bænkpres", "80", "3", "10", "50");
Close();
}
private void btnAnuller_Click(object sender, RoutedEventArgs e)
{
Close();
}
}
}
This is because you are creating a new MainWindow instead of using the initial one that is calling the Child Window.
A simple fix could be to pass a reference of the Parent MainWindow into the Child like so:
Update the child:
public partial class Tilføj_Øvelse : Window
{
MainWindow mw;
public Tilføj_Øvelse(MainWindow mainWindow)
{
InitializeComponent();
mw = mainWindow;
}
private void btnTilføj_Click(object sender, RoutedEventArgs e)
{
mw.AddRow("Bænkpres", "80", "3", "10", "50");
Close();
}
private void btnAnuller_Click(object sender, RoutedEventArgs e)
{
Close();
}
}
and then update the call to it:
private void btnTilføj_Click(object sender, RoutedEventArgs e)
{
var tilføjØvelse = new Tilføj_Øvelse(this);
tilføjØvelse.Show();
}
Though, the best (and recommended) way is to use the MVVM pattern and create a shared ViewModel for both of these Windows.

Categories

Resources