sharepoint SPFarm.local.GetObject return null - sharepoint

I installed sharepoint foundation 2010 (in 'Standalone' type) on windows 2012 r2.
And I created a windows app project.
When Load , New Counter(farm) is success.
But, SPFarm.local.GetObject return null;
Does someone konw the reason,please help me.
source like this:
//-------source start-------
//Counter.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
namespace WinAppTest
{
class Counter : SPPersistedObject
{
public static String SettingName = "Counter";
public static String SettingGuid = "62648e50-8aee-42b2-b074-2f49ced85587";
[Persisted]
public String name;
[Persisted]
public String count;
public Counter()
{
}
public Counter(SPPersistedObject parent)
: base(SettingName, parent, new Guid(SettingGuid))
{
}
}
}
// Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.SharePoint.Administration;
namespace WinAppTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Write_Click(object sender, EventArgs e)
{
SPFarm farm = SPFarm.Local;
//------- *conf is null*-----
Counter conf = (Counter)farm.GetObject(new Guid(Counter.SettingGuid));
//------- *conf is null*-----
conf.Name = "pf1";
conf.count = "1";
conf.Update();
}
private void Form1_Load(object sender, EventArgs e)
{
SPFarm farm = SPFarm.Local;
Counter conf = (Counter)farm.GetObject(new Guid(Counter.SettingGuid));
if (conf == null)
{
conf = new Counter(farm);
}
//------- *conf is null*-----
conf = (Counter)farm.GetObject(new Guid(Counter.SettingGuid));
//------- *conf is null*-----
String name = conf.name;
String count = conf.count;
}
}
}

Finally , I think the source has error in it.
modify like this, It's OK.
private void Write_Click(object sender, EventArgs e)
{
SPFarm farm = SPFarm.Local;
Counter conf = (Counter)farm.GetObject(new Guid(Counter.SettingGuid));
if (conf == null)
{
conf = new Counter(farm);
}
conf.Name = "pf1";
conf.count = "1";
conf.Update();
}
private void Form1_Load(object sender, EventArgs e)
{
SPFarm farm = SPFarm.Local;
Counter conf = (Counter)farm.GetObject(new Guid(Counter.SettingGuid));
String name = conf.name;
String count = conf.count;
}

Related

Xamarin Forms adding a placeholder to an Editor for iOS

How would you add a placeholder to an Editor in Xamarin Forms for iOS?
I tried adding through custom renderer as Control.Layer but could not find a property related to it.
Please help.
Try following code:
PCL:
using System;
using Xamarin.Forms;
namespace ABC.CustomViews
{
public class PlaceholderEditor : Editor
{
public static readonly BindableProperty PlaceholderProperty =
BindableProperty.Create<PlaceholderEditor, string>(view => view.Placeholder, String.Empty);
public PlaceholderEditor()
{
}
public string Placeholder
{
get
{
return (string)GetValue(PlaceholderProperty);
}
set
{
SetValue(PlaceholderProperty, value);
}
}
}
}
iOS (CustomeRenderer) :
using UIKit;
using ABC.CustomViews;
using ABC.iOS.CustomRenderer;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
[assembly: ExportRenderer(typeof(PlaceholderEditor), typeof(PlaceholderEditorRenderer))]
namespace ABC.iOS.CustomRenderer
{
public class PlaceholderEditorRenderer : EditorRenderer
{
private string Placeholder { get; set; }
protected override void OnElementChanged(ElementChangedEventArgs<Editor> e)
{
base.OnElementChanged(e);
var element = this.Element as PlaceholderEditor;
if (Control != null && element != null)
{
Placeholder = element.Placeholder;
Control.TextColor = UIColor.LightGray;
Control.Text = Placeholder;
Control.ShouldBeginEditing += (UITextView textView) =>
{
if (textView.Text == Placeholder)
{
textView.Text = "";
textView.TextColor = UIColor.Black; // Text Color
}
return true;
};
Control.ShouldEndEditing += (UITextView textView) =>
{
if (textView.Text == "")
{
textView.Text = Placeholder;
textView.TextColor = UIColor.LightGray; // Placeholder Color
}
return true;
};
}
}
}
}
Usage :
_replyEditor = new PlaceholderEditor
{
Placeholder = "Placeholder Text"
};
below is the code for android
using System;
using MyApp;
using MyApp.Droid;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
[assembly: ExportRenderer(typeof(CustomEditor), typeof(CustomEditorRenderer))]
namespace MyApp.Droid
{
public class CustomEditorRenderer : EditorRenderer
{
public CustomEditorRenderer()
{
}
protected override void OnElementChanged(ElementChangedEventArgs<Editor> e)
{
base.OnElementChanged(e);
if (e.NewElement != null)
{
var element = e.NewElement as CustomEditor;
this.Control.Hint = element.Placeholder;
Control.Gravity = Android.Views.GravityFlags.Start;
Control.SetBackgroundColor(global::Android.Graphics.Color.White);
Control.SetPadding(15,15,15,15);
}
}
}
Here is a Xamarin forms version that allows a placehodler to be included in the initializer of the Editor, and also handles consistent behavior if the Text property is set in code (i.e. if Editor.Text="" it will show the placeholder in light gray.
using System;
using Xamarin.Forms;
namespace CrowdWisdom.Controls
{
public class EditorPlaceHolder : Editor
{
String placeHolderText = "";
public EditorPlaceHolder(String placeholder)
{
Text = placeholder;
TextColor = Color.LightGray;
this.Focused += EditorPlaceHolder_Focused;
this.Unfocused += EditorPlaceHolder_Unfocused;
this.placeHolderText = placeholder;
}
private void EditorPlaceHolder_Focused(object sender, FocusEventArgs e) //triggered when the user taps on the Editor to interact with it
{
if (Empty())
{
base.Text = "";
this.TextColor = Color.Black;
}
}
private void EditorPlaceHolder_Unfocused(object sender, FocusEventArgs e) //triggered when the user taps "Done" or outside of the Editor to finish the editing
{
if (Empty()) //if there is text there, leave it, if the user erased everything, put the placeholder Text back and set the TextColor to gray
{
this.Text = placeHolderText;
this.TextColor = Color.LightGray;
}
}
public String PlaceHolderText
{
get
{
return this.placeHolderText;
}
set
{
if (this.Empty())
{
this.Text = value;
this.TextColor = Color.LightGray;
}
this.placeHolderText = value;
}
}
public bool Empty()
{
return (this.Text.Equals("") || this.Text.Equals(this.placeHolderText));
}
public virtual new string Text
{
get
{
return base.Text;
}
set
{
base.Text = value;
if (Empty())
{
this.TextColor = Color.LightGray;
base.Text = this.placeHolderText;
}
else
{
this.TextColor = Color.Black;
}
}
}
}
}
Xamarin Forms adding a placeholder to an Editor for Android
using System;
using System.Collections.Generic;
using System.Text;
using Xamarin.Forms;
namespace MyCare.Renderers
{
class PlaceholderEditor : Editor
{
public static readonly BindableProperty PlaceholderProperty = BindableProperty.Create<PlaceholderEditor, string>(view => view.Placeholder, String.Empty);
public PlaceholderEditor()
{
}
public string Placeholder
{
get
{
return (string)GetValue(PlaceholderProperty);
}
set
{
SetValue(PlaceholderProperty, value);
}
}
}
}
//Renderer
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Xamarin.Forms;
using MyCare.Renderers;
using MyCare.Droid.Renderers;
using Xamarin.Forms.Platform.Android;
[assembly: ExportRenderer(typeof(PlaceholderEditor), typeof(PlaceholderEditorRenderer))]
namespace MyCare.Droid.Renderers
{
class PlaceholderEditorRenderer : EditorRenderer
{
private string Placeholder { get; set; }
protected override void OnElementChanged(ElementChangedEventArgs<Editor> e)
{
base.OnElementChanged(e);
var element = this.Element as PlaceholderEditor;
if (Control != null && element != null)
{
Placeholder = element.Placeholder;
Control.SetTextColor(Android.Graphics.Color.Black);
Control.SetHintTextColor(Android.Graphics.Color.LightGray);
Control.Hint = element.Placeholder;
Control.SetBackgroundColor(Android.Graphics.Color.Transparent);
}
}
}
}
Inherit from Jay's solution. Here is a usage in XAML
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:cstCtrl="clr-namespace:TeamCollaXform.Views"
x:Class="TeamCollaXform.Views.MailComposePage">
...
<cstCtrl:PlaceholderEditor Grid.Row="2" x:Name="txtContent" Text="" HeightRequest="750" Placeholder="Compose..."/>

Error in save video from webcam video c#

I need some help with error..
Error 2 The type 'AForge.Video.NewFrameEventArgs' exists in both 'c:\Users\anton\OneDrive\Документы\Visual Studio 2013\Projects\WindowsFormsApplication4\packages\Accord.Video.3.0.2\lib\net45\Accord.Video.dll' and 'c:\Users\anton\OneDrive\Documents\Visual Studio 2013\Projects\WindowsFormsApplication4\packages\AForge.Video.2.2.5\lib\AForge.Video.dll' C:\Users\anton\onedrive\Documents\visual studio 2013\Projects\WindowsFormsApplication4\WindowsFormsApplication4\Form1.cs 50 42 WindowsFormsApplication4
this is my code:`
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using AForge.Video;
using AForge.Video.DirectShow;
using AForge.Video.FFMPEG;
namespace WindowsFormsApplication4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private VideoCaptureDevice FinalVideo = null;
private VideoCaptureDeviceForm captureDevice;
private Bitmap video;
private VideoFileWriter FileWriter = new VideoFileWriter();
private SaveFileDialog saveAvi;
private FilterInfoCollection webcam;
private VideoCaptureDevice cam;
private void Form1_Load(object sender, EventArgs e)
{
webcam = new FilterInfoCollection(FilterCategory.VideoInputDevice);
foreach (FilterInfo VideoCaptureDevice in webcam)
{
comboBox1.Items.Add(VideoCaptureDevice.Name);
comboBox1.SelectedIndex = 0;
}
pictureBox1.Anchor = (AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Bottom);
comboBox1.Anchor = (AnchorStyles.Bottom);
button1.Anchor = (AnchorStyles.Bottom);
button2.Anchor = (AnchorStyles.Bottom);
button3.Anchor = (AnchorStyles.Bottom);
}
private void button1_Click(object sender, EventArgs e)
{
cam = new VideoCaptureDevice(webcam[comboBox1.SelectedIndex].MonikerString);
cam.NewFrame += new NewFrameEventHandler(cam_NewFrame);
cam.Start();
}
void cam_NewFrame(object sender, NewFrameEventArgs EventArgs)
{
Bitmap bit = (Bitmap)EventArgs.Frame.Clone();
pictureBox1.Image = bit;
}
private void button3_Click(object sender, EventArgs e)
{
if (cam.IsRunning)
{
cam.Stop();
}
}
private void button2_Click(object sender, EventArgs e)
{
saveFileDialog1.InitialDirectory = #"C:\picture";
saveFileDialog1.Filter = "Images|*.png;*.bmp;*.jpg";
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
pictureBox1.Image.Save(saveFileDialog1.FileName);
}
}
private void button4_Click(object sender, EventArgs e)
{
saveAvi = new SaveFileDialog();
saveAvi.Filter = "Avi Files (*.avi)|*.avi";
if (saveAvi.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
int h = captureDevice.VideoDevice.VideoResolution.FrameSize.Height;
int w = captureDevice.VideoDevice.VideoResolution.FrameSize.Width;
FileWriter.Open(saveAvi.FileName, w, h, 25, VideoCodec.Default, 5000000);
FileWriter.WriteVideoFrame(video);
//AVIwriter.Open(saveAvi.FileName, w, h);
//FinalVideo = captureDevice.VideoDevice;
//FinalVideo.NewFrame += new NewFrameEventHandler(FinalVideo_NewFrame);
//FinalVideo.Start();
}
}
}
}

Edit Excel Column

using System;
using System.Data;
using System.Windows.Forms;
using System.Data.OleDb;
using System.IO;
using System.Text;
namespace SDM
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void browsefiles_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog()==DialogResult.OK)
{
display(openFileDialog1.FileName);
}
}
private void display(string text)
{
textBox1.Text = string.Format("{0}", openFileDialog1.FileName);
}
private void importFile_Click(object sender, EventArgs e)
{
string PathConn = ("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + textBox1.Text + "; Jet OLEDB:Engine Type=5;Extended Properties=\"Excel 12.0;\"");
OleDbConnection conn= new OleDbConnection(PathConn);
OleDbDataAdapter myDataAdapter = new OleDbDataAdapter("Select * from [" + textBox2.Text+ "$]", conn);
DataTable dt = new DataTable();
myDataAdapter.Fill(dt);
if (textBox2.Text.Contains("Life Group"))
{
dt.Columns.Add("Agent Code", typeof(string));
}
else if (textBox2.Text.Contains("Life Individual"))
{
DataColumn col = dt.Columns[9].ToString();
String.Format("####-###");
Console.WriteLine(col);
myDataAdapter.Fill(dt);
}
I am trying to change the format of Column [9] from "1234567" to "1234-567". I just want to add a dash after the 4th number but am having trouble calling on that entire column to make the change. Can anybody lend any suggestions? It would be greatly appreciated.

OxyPlot EventTrigger

I am trying to handle event in OxyPlot using EventTrigger. But it does not work like I want.
<Window x:Class="WpfApplication14.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:oxy="http://oxyplot.org/wpf"
Title="MainWindow" Height="350" Width="525">
<Grid>
<oxy:PlotView Model="{Binding PlotModel}" DefaultTrackerTemplate="{x:Null}">
<i:Interaction.Triggers >
<i:EventTrigger EventName="MouseDown">
<i:InvokeCommandAction Command="{Binding Move}"></i:InvokeCommandAction>
</i:EventTrigger>
</i:Interaction.Triggers>
</oxy:PlotView>
</Grid>
And code look's like
using OxyPlot;
using System;
using System.Collections.Generic;
using System.Diagnostics;
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 OxyPlot.Axes;
using GalaSoft.MvvmLight.Command;
namespace WpfApplication14
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
///
public partial class MainWindow : Window
{
public class DataGeneration
{
public PlotModel PlotModel { get; set; }
public ICommand Move { get; set; }
public int _NumberOf;
public Dictionary<int, List<int>> Test { get; set; }
OxyPlot.Series.LineSeries LS;
Random rnd;
LinearAxis LA;
public int EndPosition = 0;
public DataGeneration(int NumberOf)
{
LA = new LinearAxis()
{
Position=AxisPosition.Bottom,
Minimum=0,
Maximum = EndPosition
};
Move = new RelayCommand(() => TestMove());
_NumberOf = NumberOf;
rnd = new Random();
Test = new Dictionary<int, List<int>>();
PlotModel = new PlotModel();
PlotModel.Axes.Add(LA);
LS = new OxyPlot.Series.LineSeries();
PlotModel.Series.Add(LS);
}
public void TestMove()
{
PlotModel.Axes[0].Reset();
}
public void AddPoint()
{
do
{
LS.Points.Add(new DataPoint(++_NumberOf, rnd.Next(1, 10)));
System.Threading.Thread.Sleep(1000);
EndPosition += 1;
LA.Maximum = EndPosition+3;
LA.Minimum = EndPosition;
Update();
} while (true);
}
public void Update()
{
PlotModel.InvalidatePlot(true);
}
}
public delegate void BeginUpdate();
private Stopwatch stopwatch = new Stopwatch();
private long lastUpdateMilliSeconds;
DataGeneration Data;
public MainWindow()
{
Data = new DataGeneration(1);
BeginUpdate BU = new BeginUpdate(Data.AddPoint);
IAsyncResult result = BU.BeginInvoke(null, null);
this.DataContext = Data;
CompositionTarget.Rendering += CompositionTargetRendering;
InitializeComponent();
}
private void CompositionTargetRendering(object sender, EventArgs e)
{
if (stopwatch.ElapsedMilliseconds > lastUpdateMilliSeconds + 300)
{
Data.Update();
}
}
}
}
why it firing when I make double click on OxyPlot? Genaraly i am trying to pan chart by left mousebutton.It's implemented in oxyplot by default(right mouse button).

Switching to another application (previously active) on button click in .net

Hi I want to do something like on screen keyboard. I want user to click a button on inactive application and then key press will be sent to active application while keeping active application active. I wrote the code for hover event of the button in inactive application and it is working. But what I want is to do it in click event. It is not working because inactive application becomes active. The code is below for hover event. Thank you.
private void button1_MouseHover(object sender, EventArgs e)
{
SendKeys.Send("{TAB}");
}
Finally I could figure out a way to do it.
Refer the code below.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Timers;
using tmr = System.Timers;
using System.Threading;
namespace KeyPressTest
{
public partial class Form1 : Form
{
[DllImport("user32.dll")]
static extern int GetForegroundWindow();
[DllImport("user32")]
private static extern UInt32 GetWindowThreadProcessId(Int32 hWnd, out Int32 lpdwProcessId);
[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
tmr.Timer tm = new tmr.Timer();
Int32 hwnd = 0;
private Int32 GetWindowProcessID(Int32 hwnd)
{
Int32 pid = 1;
GetWindowThreadProcessId(hwnd, out pid);
return pid;
}
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
tm.Elapsed += Timer_Elapsed;
tm.Interval = 100;
tm.Start();
}
private void button1_Click(object sender, EventArgs e)
{
SetForegroundWindow((IntPtr)hwnd);
Thread.Sleep(40);
SendKeys.Send("{TAB}");
}
private void Timer_Elapsed(object sender, System.EventArgs args)
{
if (this.InvokeRequired)
{
this.Invoke(new MethodInvoker(delegate
{
if (this.Handle.ToInt32() != GetForegroundWindow())
{
hwnd = GetForegroundWindow();
}
}));
}
else
{
if (this.Handle.ToInt32() != GetForegroundWindow())
{
hwnd = GetForegroundWindow();
}
}
if (hwnd == 0)
{
return;
}
string appProcessName = "";
string appExePath = "";
string appExeName = "";
try
{
appProcessName = Process.GetProcessById(GetWindowProcessID(hwnd)).ProcessName;
appExePath = Process.GetProcessById(GetWindowProcessID(hwnd)).MainModule.FileName;
appExeName = appExePath.Substring(appExePath.LastIndexOf(#"\") + 1);
}
catch (Exception ex)
{
}
if (textBox1.InvokeRequired)
{
textBox1.Invoke(new MethodInvoker(delegate
{
textBox1.Text = appProcessName + " | " + appExePath + " | " + appExeName;
}));
}
else
{
textBox1.Text = appProcessName + " | " + appExePath + " | " + appExeName;
}
}
}
}
I have never done something similar, but here is what I found in this forum:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
[DllImport("user32.dll")]
static extern int GetForegroundWindow();
[DllImport("user32")]
private static extern UInt32 GetWindowThreadProcessId(Int32 hWnd, out Int32 lpdwProcessId);
private int teller = 0;
public Form1()
{
InitializeComponent();
}
private void timer1_Tick(object sender, EventArgs e)
{
if (teller == 1)
{
setTextje();
}
teller++;
}
private Int32 GetWindowProcessID(Int32 hwnd)
{
Int32 pid = 1;
GetWindowThreadProcessId(hwnd, out pid);
return pid;
}
private void setTextje()
{
Int32 hwnd = 0;
hwnd = GetForegroundWindow();
string appProcessName = Process.GetProcessById(GetWindowProcessID(hwnd)).ProcessName;
string appExePath = Process.GetProcessById(GetWindowProcessID(hwnd)).MainModule.FileName;
string appExeName = appExePath.Substring(appExePath.LastIndexOf(#"\") + 1);
textBox1.Text = appProcessName + " | " + appExePath + " | " + appExeName;
}
}
}
It doesn't answer exactly to your question but it will give you a hint. You need to DllImport "User32.dll". After that you can get the id of the foreground window and play with that.
There is also a very interesting article about application switch tracking written in C#

Resources