Xamarin.Forms: NavigationController is null in PageRenderer - c#

I am trying to use PageRenderer to customize/reposition elements of ToolbarItem for iOS but here NavigationController throwing null reference exception.
Below my code
public class MyNavigationRenderer: PageRenderer
{
public new MyNavigationBar Element
{
get { return (MyNavigationBar)base.Element; }
}
public override void ViewWillAppear(bool animated)
{
base.ViewWillAppear(animated);
var LeftNavList = new List<UIBarButtonItem>();
var rightNavList = new List<UIBarButtonItem>();
var navigationItem = this.NavigationController.TopViewController.NavigationItem;
for (var i = 0; i < Element.ToolbarItems.Count; i++)
{
var reorder = (Element.ToolbarItems.Count - 1);
var ItemPriority = Element.ToolbarItems[reorder - i].Priority;
if (ItemPriority == 1)
{
UIBarButtonItem LeftNavItems = navigationItem.RightBarButtonItems[i];
LeftNavList.Add(LeftNavItems);
}
else if (ItemPriority == 0)
{
UIBarButtonItem RightNavItems = navigationItem.RightBarButtonItems[i];
rightNavList.Add(RightNavItems);
}
}
navigationItem.SetLeftBarButtonItems(LeftNavList.ToArray(), false);
navigationItem.SetRightBarButtonItems(rightNavList.ToArray(), false);
}
}
Below MyNavigationBar.cs class in portable/shared forms project
public class MyNavigationBar : NavigationPage
{
public MyNavigationBar(Page content) : base(content)
{
Init();
}
private void Init()
{
this.ToolbarItems.Add(new ToolbarItem() { Icon = "kid", Priority = 0, Order = ToolbarItemOrder.Primary });
this.ToolbarItems.Add(new ToolbarItem() { Text = "License", Priority = 0, Order = ToolbarItemOrder.Primary });
}
}
App starting
public App ()
{
InitializeComponent();
MainPage = new MyNavigationBar(new LoginPage());
}
See below screenshot getting exception

I faced this issue, but in my case, I was trying to get NavigationController from content page which didn't had NavigationController, make sure you null check before calling TopViewController,
var navController = this.NavigationController;
if(navController == null)
{
return;
}
UINavigationItem navigationItem = navController.TopViewController.NavigationItem;
For example,
When User opens the app, he will be presented with Login page, which didn't had any Navigation Bar.

Related

Xamarin Android: How to delete multiple itens in recyclerview

Hello everyone my name is Taniguchi
I've implemented a recyclerview and inserted a button navigation view to delete selected items.
When i click on a item in the recyclerview i select a item. I inserted a collection to store the item position when i click and when the item is unselected i remove the item position from collection
I was able to delete multiple itens on recyclerview but one item is being deleted wrong, for example. I select two itens position 0 and position 1 i click to delete both, but instead of delete position 1, the item position 3 is removed instead and the postion 2 item continues to be selected.
How can i remove itens properly ?
my recyclerview adapter:
public class RecyclerAdapter : RecyclerView.Adapter, View.IOnClickListener
{
private List<Email> mEmails;
private Activity mActivity;
public static int listPosition;
private List<collectPosition> mcollectID;
private int position_collectID = 0;
private Boolean isSelected = false;
public Boolean IsSelected()
{
return isSelected;
}
public void setSelected(Boolean selected)
{
isSelected = selected;
}
public RecyclerAdapter(List<Email> emails, Activity activity)
{
mEmails = emails;
mActivity = activity;
}
public override int ItemCount => mEmails.Count;
public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
{
View row = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.row, parent, false);
RecyclerViewHolder view = new RecyclerViewHolder(row);
return view;
}
public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
{
RecyclerViewHolder myHolder = holder as RecyclerViewHolder;
myHolder.mDate.Text = ((Email)mEmails[position]).date;
myHolder.mSubject.Text = ((Email)mEmails[position]).Subject;
myHolder.mMessage.Text = ((Email)mEmails[position]).Message;
myHolder.ItemView.SetBackgroundColor(((Email)mEmails[position]).IsSelected() ? Color.LightBlue : Color.Transparent);
myHolder.ItemView.Tag = position;
myHolder.ItemView.SetOnClickListener(this);
}
void View.IOnClickListener.OnClick(View v)
{
int position = (int)v.Tag;
listPosition = (int)v.Tag;
((Email)mEmails[position]).setSelected(!((Email)mEmails[position]).IsSelected());
v.SetBackgroundColor(((Email)mEmails[position]).IsSelected() ? Color.LightBlue : Color.Transparent);
if (mcollectID == null)
mcollectID = new List<collectPosition>();
if (((Email)mEmails[position]).IsSelected())
{
mcollectID.Add(new collectPosition() { getPosition = (int)v.Tag });
position_collectID = position_collectID + 1;
}
else
{
position_collectID = position_collectID - 1;
mcollectID.RemoveAt(position_collectID);
}
Toast.MakeText(v.Context, "Click : " + ((Email)mEmails[position]).date + "---" + position, ToastLength.Short).Show();
}
public void DeleteItem()
{
if (mEmails != null && mEmails.Count > 0)
{
foreach (collectPosition collId in mcollectID)
{
mEmails.RemoveAt(collId.getPosition);
NotifyItemChanged(collId.getPosition);
NotifyDataSetChanged();
}
}
}
}
where i calling the recyclerview:
public class MainActivity : AppCompatActivity
{
public static RecyclerView mRecyclerView;
public static RecyclerView.LayoutManager mLayoutManager;
public RecyclerView.ViewHolder holder;
public static BottomNavigationView bottomnavigationview1;
public RecyclerAdapter mAdapter;
public static List<Email> mEmails;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
Xamarin.Essentials.Platform.Init(this, bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.activity_main);
mRecyclerView = FindViewById<RecyclerView>(Resource.Id.recyclerViwer);
mRecyclerView.AddItemDecoration(new DividerItemDecoration(mRecyclerView.Context, DividerItemDecoration.Vertical));
mRecyclerView.HasFixedSize = true;
SetupList();
//Create our layout Manager
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.SetLayoutManager(mLayoutManager);
mAdapter = new RecyclerAdapter(mEmails, this);
mRecyclerView.SetAdapter(mAdapter);
bottomnavigationview1 = FindViewById<BottomNavigationView>
(Resource.Id.bottom_navigation);
bottomnavigationview1.NavigationItemSelected += BottomNavigation_NavigationItemSelected;
}
private void SetupList()
{
mEmails = new List<Email>();
mEmails.Add(new Email() { date = "9/25/2019", Subject = "Wanna Hang Out?", Message = "I ' ll be around tomorrow!!" });
mEmails.Add(new Email() { date = "9/25/2019", Subject = "Wanna Hang Out?", Message = "I ' ll be around tomorrow!!" });
mEmails.Add(new Email() { date = "9/25/2019", Subject = "Wanna Hang Out?", Message = "I ' ll be around tomorrow!!" });
mEmails.Add(new Email() { date = "9/24/2019", Subject = "Wanna Hang Out?", Message = "I ' ll be around tomorrow!!" });
mEmails.Add(new Email() { date = "9/24/2019", Subject = "Wanna Hang Out?", Message = "I ' ll be around tomorrow!!" });
}
if you just want to delete the items which you select,you don't need to store the position,as you have saved the check status,the property IsSelected in your Email model.
you could use it directly like :
void View.IOnClickListener.OnClick(View v)
{
int position = (int)v.Tag;
listPosition = (int)v.Tag;
((Email)mEmails[position]).setSelected(!((Email)mEmails[position]).IsSelected());
v.SetBackgroundColor(((Email)mEmails[position]).IsSelected() ? Color.LightBlue : Color.Transparent);
Toast.MakeText(v.Context, "Click : " + ((Email)mEmails[position]).date + "---" + position, ToastLength.Short).Show();
}
public void DeleteItem()
{
if (mEmails != null && mEmails.Count > 0)
{
foreach (Email email in mEmails.ToList())
{
if (email.IsSelected())
{
mEmails.Remove(email);
}
}
NotifyDataSetChanged();
}
}

Xamarin: Open Dialer with Tel: Links in WKWebView

In a WKWebView, clicking on the Tel: links (example: ) does not open the phone dialer with the number from the link as they do in Chrome/Safari.
I have looked at the solution from the link below:
https://forums.xamarin.com/discussion/103689/after-ios-11-upgrade-wkwebview-does-not-load-my-website
However, in my C# project, I am unable to use two base classes (UIViewController, WKNavigationDelegate) in my class as my class WebViewController cannot have multiple base classes.
Is it possible to do this in the DidFinishNavigation method to open the dialer when Tel: links are clicked?
My full code is below with changes that mimics the idea from the link above. Would it be possible for me to achieve this with the way my web view is designed?
[Register("WebViewController")]
public class WebViewController : UIViewController
{
public override void ViewDidLoad()
{
base.ViewDidLoad();
WKWebView webView = new WKWebView(View.Frame, new
WKWebViewConfiguration());
View.AddSubview(webView);
View.SendSubviewToBack(webView);
webView.AutoresizingMask = UIViewAutoresizing.FlexibleDimensions;
var url = new NSUrl("link goes here");
var request = new NSUrlRequest(url);
webView.LoadRequest(request);
webView.AllowsBackForwardNavigationGestures = true;
}
public override void ViewWillAppear(bool animated)
{
base.ViewWillAppear(animated);
NavigationController.NavigationBarHidden = true;
}
//open email and tel links
// https://forums.xamarin.com/discussion/103689/after-ios-11-upgrade-wkwebview-does-not-load-my-website
//https://forums.xamarin.com/discussion/47335/how-to-call-a-set-phone-number-from-a-button-click-using-xamarin-ios
[Export("webView:didFinishNavigation:")]
//[Export("webView:decidePolicyForNavigationAction:decisionHandler:")]
void DidFinishNavigation(WKWebView webView, WKNavigation navigation, WKNavigationAction navigationAction, Action<WKNavigationActionPolicy> decisionHandler)
{
var navType = navigationAction.NavigationType;
var targetFrame = navigationAction.TargetFrame;
var url = navigationAction.Request.Url;
if (
(url.ToString().StartsWith("http") && targetFrame == null)
||
url.ToString().StartsWith("mailto:")
|| url.ToString().StartsWith("tel:")
|| url.ToString().StartsWith("Tel:"))
{
UIApplication.SharedApplication.OpenUrl(url);
}
}
}
}
Fixed it by adding a custom navigation delegate class:
public override void ViewDidLoad()
{
base.ViewDidLoad();
WKWebView webView = new WKWebView(View.Frame, new WKWebViewConfiguration());
View.AddSubview(webView);
View.SendSubviewToBack(webView);
webView.AutoresizingMask = UIViewAutoresizing.FlexibleDimensions;
var url = new NSUrl("link");
var request = new NSUrlRequest(url);
webView.LoadRequest(request);
webView.AllowsBackForwardNavigationGestures = true;
//assign delegate
webView.NavigationDelegate = new MyWKNavigationDelegate();
}
//custom delegate
class MyWKNavigationDelegate : WKNavigationDelegate
{
[Export("webView:decidePolicyForNavigationAction:decisionHandler:")]
public override void DecidePolicy(WKWebView webView, WKNavigationAction
navigationAction, Action<WKNavigationActionPolicy> decisionHandler)
{
var navType = navigationAction.NavigationType;
var targetFrame = navigationAction.TargetFrame;
var url = navigationAction.Request.Url;
if (
url.ToString().StartsWith("http") && (targetFrame != null &&
targetFrame.MainFrame == true)
)
{
decisionHandler(WKNavigationActionPolicy.Allow);
}
else if (
//(url.ToString().StartsWith("http") && targetFrame == null)
//||
url.ToString().StartsWith("mailto:")
|| url.ToString().StartsWith("tel:")
|| url.ToString().StartsWith("Tel:"))
{
//decisionHandler(WKNavigationActionPolicy.Allow);
UIApplication.SharedApplication.OpenUrl(url);
}
}
}

How to get the clicked item from MpAndroidChart(UltimateXF) in Xamarin forms

I'm using MpAndroidChart(UltimateXF) for my app. I created the piechart successfully but I am unable to create a event listener to get the clicked item when user touched one of piechart item.
This is my UltimateXF library
https://nuget.org/packages/UltimateXF/
This is my code....
public partial class HomePage : ContentPage
{
public HomePage ()
{
InitializeComponent ();
var entries = new List<PieEntry>();
entries.Add(new PieEntry(40, "Life"));
entries.Add(new PieEntry(60, "General"));
var dataSet4 = new PieDataSet(entries, "")
{
Colors = new List<Color>()
{
Color.Accent, Color.Chocolate
},
ValueLineColor = Color.Blue,
SliceSpace = 2f,
ValueFormatter = new CustomPercentDataSetValueFormatter()
};
var data4 = new PieChartData(dataSet4)
{
};
var dataSet5 = new PieDataSet(entries, "")
{
};
pieChart.ChartData = data4;
pieChart.RotateEnabled = false;
}
}
public class CustomPercentDataSetValueFormatter : IDataSetValueFormatter
{
public string GetFormattedValue(float value, int dataSetIndex)
{
return value + "%";
}
}

WPF MVVM Control that connects to Web API

I have an ASP.NET Web API with SQL Database / Entity Framework that I will be using as the server side of an application that I am building. The client side will be WPF for now. I am having some trouble determining the best method for the MVVM controls to access the data from the server. All of the tutorials and courses I have done for MVVM connect directly to a local database, and they update when the control is loaded. What I want is a constant loop where the API is called once per minute and the data in the control automatically refreshed at that point. I have this working for the two main controls (ran into a problem updating the collection from a different thread and had to use App.Current.Dispatcher.Invoke), however I am not sure if I am keeping to best practice running the API calls directly from the view model. Also, I need to be able to share data between the view models.
ScheduledForwardsViewModel has data that ActiveForwardsViewModel needs, and ActiveForwardsViewModel has data that CreateForwardsViewModel needs. The ActiveForward for each entry needs to be able to see the scheduled date for each corresponding scheduled item so it can find the next event and display the time left until that event occurs in the ActiveForwardsView. The CreateForwardView simply needs access to the AllUsers,AllGroups,AllContacts observable collections created during the ActiveForwards data manipulation so it can use that data in the drop down fields when scheduling a new forward. I haven't been able to get the dropdowns to work yet. I do have access to the info I need from ScheduledForwardsView using a static object, but I feel like there may be a better way. I would prefer to refrain from making multiple calls to the API for the same data when part of my app already has the data I need.
Basic Layout:
MainWindow
CreateForwardView (with view model)
[Drop down boxes to select user and schedule forward]
[Buttons, etc.]
[TabControl]
[TabItem: Active Forwards]
ActiveForwardsView (with view model)
[TabItem: Scheduled Forwards]
ScheduledForwardsView (with view model)
[EndTabControl]
The ActiveForwardsViewModel is below. The ScheduledForwardsViewModel is essentially the same thing, but it calls a different API method and contains a static object that I am using in ActiveForwardsViewModel (see the part where I wait for ScheduledForwardsViewModel to complete 1 run before I continue with UpdateUserObjects). That didn't seem like the right way to do it, but I set it up that way just so I could move onto other things.
public class ActiveForwardsViewModel : INotifyPropertyChanged
{
private CancellationToken updater = default(CancellationToken);
private AppLog log;
private FFObject _selectedObject;
private ObservableCollection<FFObject> _activeForwards;
public ObservableCollection<FFObject> ActiveForwards
{
get { return _activeForwards; }
set
{
if (_activeForwards != value)
{
_activeForwards = value;
PropertyChanged(this, new PropertyChangedEventArgs("ActiveForwards"));
}
}
}
public ObservableCollection<FFObject> AllUsers { get; set; }
public ObservableCollection<FFObject> AllGroups { get; set; }
public ObservableCollection<FFObject> AllContacts { get; set; }
public ObservableCollection<FFObject> AllObjects { get; set; }
public bool Running = false;
public bool FirstRunComplete = false;
public event PropertyChangedEventHandler PropertyChanged = delegate { };
public ActiveForwardsViewModel()
{
if (DesignerProperties.GetIsInDesignMode(new System.Windows.DependencyObject()))
{
updater.ThrowIfCancellationRequested();
return;
}
ActiveForwards = new ObservableCollection<FFObject>();
AllUsers = new ObservableCollection<FFObject>();
AllGroups = new ObservableCollection<FFObject>();
AllContacts = new ObservableCollection<FFObject>();
AllObjects = new ObservableCollection<FFObject>();
StartUpdater(updater);
RemoveForwardCommand = new RelayCommand(OnRemove, CanRemove);
}
public async Task StartUpdater(CancellationToken token = default(CancellationToken))
{
while (!token.IsCancellationRequested)
{
Running = true;
await this.Update();
FirstRunComplete = true;
try
{
await Task.Delay(TimeSpan.FromMinutes(1), token);
}
catch (TaskCanceledException)
{
Running = false;
break;
}
}
}
private List<FFObject> UpdateAllObjectList(string strJson)
{
var serializer = new JavaScriptSerializer();
return serializer.Deserialize<List<FFObject>>(strJson);
//return AllObjects;
}
public string GetString(string method)
{
using (var client = new WebClient())
{
var url = string.Format("https://mywebapi.domain.com/api/{0}", method);
return client.DownloadString(url);
}
}
public async Task Update()
{
Func<string, List<FFObject>> getUsersJson = UpdateAllObjectList;
await Task<string>.Factory.StartNew(() => GetString("Users"))
.ContinueWith(antecendent => getUsersJson(antecendent.Result))
.ContinueWith(antecendent => UpdateUserObjects(antecendent.Result));
}
private void CheckRemoved(List<FFObject> all)
{
List<FFObject> RemoveObjects = new List<FFObject>();
foreach (var obj in ActiveForwards)
{
var newObj = all.FirstOrDefault(i => i.ID == obj.ID);
if (newObj == null)
{
RemoveObjects.Add(obj);
}
else
{
if (!(bool) newObj.IsForwarded)
{
RemoveObjects.Add(obj);
}
}
}
foreach (var obj in RemoveObjects)
{
App.Current.Dispatcher.Invoke((Action)delegate
{
ActiveForwards.Remove(obj);
});
}
}
private void UpdateUserObjects(List<FFObject> all)
{
Debug.WriteLine("Starting UpdateUserObject");
if (all != null)
{
AllObjects = new ObservableCollection<FFObject>(all);
CheckRemoved(all);
var x = 0;
while (!ScheduledForwardsViewModel.RunOnce && x < 5)
{
System.Threading.Thread.Sleep(1000);
x++;
}
foreach (var obj in all)
{
if (obj.ObjectType.ToLower() == "user")
{
var existing = AllUsers.FirstOrDefault(i => i.DistinguishedName == obj.DistinguishedName);
if (existing != null)
{
existing.ForwardedTo = obj.ForwardedTo;
existing.ForwardedToAd = obj.ForwardedToAd;
existing.ForwardedToDn = obj.ForwardedToDn;
existing.ForwardedToPk = obj.ForwardedToPk;
existing.ForwardedToDisplay = obj.ForwardedToDisplay;
existing.IsForwarded = obj.IsForwarded;
existing.DeliverAndRedirect = obj.DeliverAndRedirect;
existing.IsScheduled = obj.IsScheduled;
existing.TimeLeft = obj.TimeLeft;
}
else
{
//AllUsers.Add(obj);
App.Current.Dispatcher.Invoke((Action)delegate // <--- HERE
{
AllUsers.Add(obj);
});
}
if (obj.IsForwarded ?? false)
{
existing = ActiveForwards.FirstOrDefault(i => i.DistinguishedName == obj.DistinguishedName);
obj.TimeLeft = "";
var now = DateTime.Now;
var TimeRemaining = new TimeSpan?();
foreach (var schedule in ScheduledForwardsViewModel.ScheduledForwards)
{
if (schedule.DistinguishedName == obj.DistinguishedName)
{
if (schedule.StopJobStatus == "Scheduled")
{
if (TimeRemaining == null)
{
TimeRemaining = schedule.StopTime - now;
}
else
{
if (schedule.StopTime - now < TimeRemaining)
{
TimeRemaining = schedule.StopTime - now;
}
}
}
}
}
if (TimeRemaining != null)
{
var remaining = (TimeSpan)TimeRemaining;
var min = new int();
if (remaining.Seconds > 30)
{
min = remaining.Minutes + 1;
}
double m = remaining.Minutes / (double)60;
double hm = remaining.Hours + m;
double h = hm / 24;
double dh = remaining.Days + h;
if (remaining.Days > 0)
{
var daysleft = Math.Round(dh, 2);
var quarterRound = Math.Round(dh * 4, MidpointRounding.ToEven) / 4;
obj.TimeLeft = quarterRound + "d";
}
else if (remaining.Hours > 0)
{
var hoursleft = Math.Round(hm, 2);
var quarterRound = Math.Round(hm * 4, MidpointRounding.ToEven) / 4;
obj.TimeLeft = quarterRound + "h";
obj.TimeLeft = remaining.Hours + "h" + remaining.Minutes + "m";
}
else
{
if (min == 0)
{
obj.TimeLeft = "< 30s";
}
else
{
obj.TimeLeft = min + "m";
}
}
}
if (existing != null)
{
existing.ForwardedTo = obj.ForwardedTo;
existing.ForwardedToAd = obj.ForwardedToAd;
existing.ForwardedToDn = obj.ForwardedToDn;
existing.ForwardedToPk = obj.ForwardedToPk;
existing.ForwardedToDisplay = obj.ForwardedToDisplay;
existing.IsForwarded = obj.IsForwarded;
existing.DeliverAndRedirect = obj.DeliverAndRedirect;
existing.IsScheduled = obj.IsScheduled;
existing.TimeLeft = obj.TimeLeft;
}
else
{
//ActiveForwards.Add(obj);
App.Current.Dispatcher.Invoke((Action)delegate // <--- HERE
{
ActiveForwards.Add(obj);
});
}
}
}
else if (obj.ObjectType.ToLower() == "group")
{
if (obj.IsForwarded ?? false)
{
var existing = AllGroups.FirstOrDefault(i => i.DistinguishedName == obj.DistinguishedName);
if (existing != null)
{
existing.ForwardedTo = obj.ForwardedTo;
existing.ForwardedToAd = obj.ForwardedToAd;
existing.ForwardedToDn = obj.ForwardedToDn;
existing.ForwardedToPk = obj.ForwardedToPk;
existing.ForwardedToDisplay = obj.ForwardedToDisplay;
existing.IsForwarded = obj.IsForwarded;
existing.DeliverAndRedirect = obj.DeliverAndRedirect;
existing.IsScheduled = obj.IsScheduled;
existing.TimeLeft = obj.TimeLeft;
}
else
{
//AllGroups.Add(obj);
App.Current.Dispatcher.Invoke((Action)delegate // <--- HERE
{
AllGroups.Add(obj);
});
}
}
}
else if (obj.ObjectType.ToLower() == "contact")
{
if (obj.IsForwarded ?? false)
{
var existing = AllContacts.FirstOrDefault(i => i.DistinguishedName == obj.DistinguishedName);
if (existing != null)
{
existing.ForwardedTo = obj.ForwardedTo;
existing.ForwardedToAd = obj.ForwardedToAd;
existing.ForwardedToDn = obj.ForwardedToDn;
existing.ForwardedToPk = obj.ForwardedToPk;
existing.ForwardedToDisplay = obj.ForwardedToDisplay;
existing.IsForwarded = obj.IsForwarded;
existing.DeliverAndRedirect = obj.DeliverAndRedirect;
existing.IsScheduled = obj.IsScheduled;
existing.TimeLeft = obj.TimeLeft;
}
else
{
//AllContacts.Add(obj);
App.Current.Dispatcher.Invoke((Action)delegate // <--- HERE
{
AllContacts.Add(obj);
});
}
}
}
else
{
throw new NotImplementedException();
}
}
}
RunOnce = true;
}

Umbraco 4.5.2 - Creating custom umbraco Data Editor Setting Types

I can't seem to find anything on how to edit the data editor settings before umbraco 6.2 (Juno). Is there any simple way, it must be possible. If you don't understand what i mean i want to do the same as http://www.nibble.be/?p=96 - just for umbraco 4.5.2.
Thanks :)
You need to make 3 classes Class 1 DataEditor
public class DataEditor : System.Web.UI.UpdatePanel, umbraco.interfaces.IDataEditor
{
public MWCropperDataEditor(umbraco.interfaces.IData Data, string Configuration)
{
_data = Data;
}
public virtual bool TreatAsRichTextEditor
{
get { return false; }
}
public bool ShowLabel
{
get { return true; }
}
public Control Editor { get { return this; } }
public void Save()
{
this._data.Value = "data;
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
imageUpload = new FileUpload();
imageUpload.ID = "imageUpload";
//shows Image
cropImage = new System.Web.UI.WebControls.Image();
cropImage.Width = width;
cropImage.Height = height;
cropImage.ImageUrl = this._data.Value.ToString();
//Shows dropdown
locationDropDown = new DropDownList();
AddItemsToDropDown();
lblInfo = new Label();
lblInfo.Attributes.Add("id", "title" + base.ClientID);
lblCropInfo = new Label();
lblCropInfo.Text = "Crop Location: ";
base.ContentTemplateContainer.Controls.Add(lblInfo);
base.ContentTemplateContainer.Controls.Add(imageUpload);
base.ContentTemplateContainer.Controls.Add(new LiteralControl("<br/>"));
base.ContentTemplateContainer.Controls.Add(new LiteralControl("<br/>"));
base.ContentTemplateContainer.Controls.Add(lblCropInfo);
base.ContentTemplateContainer.Controls.Add(locationDropDown);
base.ContentTemplateContainer.Controls.Add(new LiteralControl("<br/>"));
base.ContentTemplateContainer.Controls.Add(new LiteralControl("<br/>"));
base.ContentTemplateContainer.Controls.Add(cropImage);
}
}
class 2 DataType
public class MWCropperDataType : umbraco.cms.businesslogic.datatype.BaseDataType, umbraco.interfaces.IDataType
{
private umbraco.interfaces.IDataEditor _Editor;
private umbraco.interfaces.IData _baseData;
private MWCropperPrevalueEditor _prevalueeditor;
public override umbraco.interfaces.IDataEditor DataEditor
{
get
{
if (_Editor == null)
_Editor = new MWCropperDataEditor(Data, ((MWCropperPrevalueEditor)PrevalueEditor).Configuration);
return _Editor;
}
}
public override umbraco.interfaces.IData Data
{
get
{
if (_baseData == null)
_baseData = new umbraco.cms.businesslogic.datatype.DefaultData(this);
return _baseData;
}
}
public override Guid Id
{
get { return new Guid("71518B4E-B1A5-11DD-A22C-8AAA56D89593"); }
}
public override string DataTypeName
{
get { return "MWCropper"; }
}
public override umbraco.interfaces.IDataPrevalue PrevalueEditor
{
get
{
if (_prevalueeditor == null)
_prevalueeditor = new MWCropperPrevalueEditor(this);
return _prevalueeditor;
}
}
}
Class 3 PrevalueEditor
public class MWCropperPrevalueEditor : System.Web.UI.WebControls.PlaceHolder, umbraco.interfaces.IDataPrevalue
{
#region IDataPrevalue Members
// referenced datatype
private umbraco.cms.businesslogic.datatype.BaseDataType _datatype;
private TextBox _txtWidth;
private TextBox _txtHeight;
public MWCropperPrevalueEditor(umbraco.cms.businesslogic.datatype.BaseDataType DataType)
{
_datatype = DataType;
setupChildControls();
}
private void setupChildControls()
{
_txtWidth = new TextBox();
_txtWidth.ID = "txtWidth";
_txtWidth.CssClass = "umbEditorTextField";
Controls.Add(_txtWidth);
_txtHeight = new TextBox();
_txtHeight.ID = "txtHeight";
_txtHeight.CssClass = "umbEditorTextField";
Controls.Add(_txtHeight);
}
public Control Editor
{
get
{
return this;
}
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
if (!Page.IsPostBack)
{
if (Configuration.Length > 0)
{
string[] value = Configuration.Split(new char[]{';'});
_txtWidth.Text = value[0];
_txtHeight.Text = value[1];
}
else
{
_txtHeight.Text = "100";
_txtWidth.Text = "100";
}
}
}
public void Save()
{
_datatype.DBType = (umbraco.cms.businesslogic.datatype.DBTypes)Enum.Parse(typeof(umbraco.cms.businesslogic.datatype.DBTypes), DBTypes.Ntext.ToString(), true);
string data = _txtWidth.Text+";"+_txtHeight.Text;
SqlHelper.ExecuteNonQuery("delete from cmsDataTypePreValues where datatypenodeid = #dtdefid",
SqlHelper.CreateParameter("#dtdefid", _datatype.DataTypeDefinitionId));
SqlHelper.ExecuteNonQuery("insert into cmsDataTypePreValues (datatypenodeid,[value],sortorder,alias) values (#dtdefid,#value,0,'')",
SqlHelper.CreateParameter("#dtdefid", _datatype.DataTypeDefinitionId), SqlHelper.CreateParameter("#value", data));
}
protected override void Render(HtmlTextWriter writer)
{
writer.WriteLine("<table>");
writer.Write("<tr><th>Width:</th><td>");
_txtWidth.RenderControl(writer);
writer.Write("</td></tr>");
writer.Write("<tr><th>Height:</th><td>");
_txtHeight.RenderControl(writer);
writer.Write("</td></tr>");
writer.Write("</table>");
}
public string Configuration
{
get
{
object conf =
SqlHelper.ExecuteScalar<object>("select value from cmsDataTypePreValues where datatypenodeid = #datatypenodeid",
SqlHelper.CreateParameter("#datatypenodeid", _datatype.DataTypeDefinitionId));
if (conf != null)
return conf.ToString();
else
return "";
}
}
#endregion
public static ISqlHelper SqlHelper
{
get
{
return Application.SqlHelper;
}
}
}
I hope this can help you get started :)
Btw this also works for umbraco 6.2
Settings are called prevalues and you need a PrevalueEditor class that implements IDataPrevalue. Have a look at an example in this blog post:
http://www.eyecatch.no/blog/my-first-umbraco-datatype---part-2-rendering-a-recaptcha-control.aspx
This nibble post concerns doing a similar thing for v4.5 and prior, there is also a link within this for an even older version that I followed a while ago and found very helpful.
http://www.nibble.be/?p=62

Categories